package chapter8;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.Action;
public class LoginRegisterAction implements Action{
private String username;
private String password;
private HttpServletRequest request;
private static Map<String, String> users = new HashMap<String, String>();
public String register() throws Exception{
request.setAttribute("info", "您已经注册成功!");
return SUCCESS;
}
public String login() throws Exception{
try{
if(getUsername().equals("bill") && getPassword().equals("1234")){
request.setAttribute("info", "登陆成功");
return SUCCESS;
}else{
request.setAttribute("info", "登录失败");
return ERROR;
}
}catch(Exception e){
return ERROR;
}
}
public String execute() throws Exception{
return SUCCESS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
}
struts.xml file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default" namespace="/myspace">
<action name="*Action" class="chapter8.LoginRegisterAction" method="{1}">
<result name="success">/chapter8/success.jsp</result>
<result name="error">/chapter8/error.jsp</result>
</action>
</package>
</struts>
jsp page:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<script type="text/javascript">
function login(){
alert("登陆");
form.action = "/loginAction.action";
alert(form.action);
form.submit();
}
function register(){
alert("注册");
form.action = "registerAction";
form.submit();
}
</script>
<s:form name="form" theme="simple" namespace="/myspace">
用户:<s:textfield name="username" /><p />
密码:<s:textfield name="password" /><p />
<s:submit value="登陆" onclick="login()" />
<s:submit value="注册" onclick="register()" />
</s:form>
</body>
</html>
point after landing alert
then prompts:
type Status report
message / loginAction.action
description The requested resource (/ loginAction.action) is not available.
------ Solution -------------- ------------------------------
landlord, is that you address the problem ah. .
you see your namespace is myspace then each request path can be coupled myspace
Also in js developed under the absolute path to the best path.
so the
form.action = "/ loginAction.action";
form.action = "registerAction";
changed
form.action = "$ {pageContext.request.contextPath} / myspace / loginAction.action";
form.action = "$ {pageContext.request.contextPath} / myspace / registerAction";
------ Solution ------------------------------------ --------
fourth floor, note the people using struts2 tag
landlord you know what causes it?
------ For reference only -------------------------------------- -
Online, trouble you heroes ~ ~ ~ ~ ~
------ For reference only --------------------- ------------------
<action name="loginAction" class="chapter8.LoginRegisterAction" method="register">
<action name="registerAction" class="chapter8.LoginRegisterAction" method="register">
solve the problem, use a wildcard before you do not have the requested action wildcards were implored once, and then changed to a wildcard in the form, no problem.
but do not know the specific reasons why?
------ For reference only -------------------------------- -------
------ For reference only ---------------------------------- -----
I do not know, the first time without a wildcard request, then switch to a wildcard, you can, do not know the environmental issues?
------ For reference only -------------------------------------- -
never encountered before, I do not know how you get, there is definitely a problem here just like that. . Investigation it yourself slowly
没有评论:
发表评论