struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<package name="p1" extends="struts-default">
<action name="login" class="cn.qyc.loGIN.Login" method="login">
<result name="success" type="redirect">/success.jsp</result>
<result name="error" type="dispatcher">/error.jsp</result>
</action>
</package>
</struts>
这里注意一下redirect重定向和dispatcher转发
web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
LoginAction
package cn.qyc.loGIN;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport{
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
ActionContext context = ActionContext.getContext();
public String login() {
if (name.equals("强月城")&&password.equals("qycssg!")) {
System.out.println("登录成功");
context.getSession().put("username", name);
context.getSession().put("password", password);
return "success";
} else {
System.out.println("登陆失败");
context.getSession().put("username", name);
return "error";
}
}
}
登陆成功页面
<body>
<%=session.getAttribute("username") %>登陆成功!
</body>
登陆页面和登陆失败页面没放