说明:所有配置均在同一个Struts.xml中
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> <!-- 动态 --> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <!-- 开发者模式 --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 第一只去参数值方式, 包括通配符 --> <action name="login*" class="com.eudsk.action.LoginOneAction" method="{1}"> <result name="ok">/ok.jsp</result> <result name="error">/error.jsp</result> </action> <!-- 第二种取值方式 DomainModel <action name="login*" class="com.eudsk.action.DomainModelAction" method="{1}"> <result name="ok">/ok2.jsp</result> <result name="error">/error2.jsp</result> </action> --> <!-- 第三种取值方式 ModelDriven <action name="login*" class="com.eudsk.action.ModelDrivenAction" method="{1}"> <result name="ok">/ok.jsp</result> <result name="error">/error.jsp</result> </action> --> </package> </struts>
1.第一种取值方式,直接取值
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="loginlogin" method="post"> <input type="text" name="username"/><br/> <input type="password" name="password"><br/> <input type="submit"> </form> </body> </html>
LoginOneAction.java
package com.eudsk.action; import com.opensymphony.xwork2.ActionSupport; /** * 第一种取值方式 * @author Maibeli * */ public class LoginOneAction extends ActionSupport{ private static final long serialVersionUID = 1L; private String username; private String password; public String login() { String state=""; if("123".equals(username) && "123".equals(password)) { state="ok"; }else{ state="error"; } return state; } //必须要set 与get 方法 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; } }
ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 登陆成功!!! ${username } | ${password } </body> </html>
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 登录失败!!${username } | ${password } </body> </html>
2.第二种取值方式 DomainModel
login2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="loginlogin" method="post"> <input type="text" name="user.username"/><br/> <input type="password" name="user.password"><br/> <input type="submit"> </form> </body> </html>
DomainModelAction.java
package com.eudsk.action; import com.eudsk.entity.User; import com.opensymphony.xwork2.ActionSupport; public class DomainModelAction extends ActionSupport{ private static final long serialVersionUID = 1L; private User user; public String login() { String state=""; if("123".equals(user.getUsername()) && "123".equals(user.getPassword())) { state="ok"; }else{ state="error"; } return state; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
ok2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 登陆成功!!! ${user.username } | ${user.password } </body> </html>
error2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 登录失败!!${user.username } | ${user.password } </body> </html>
所需实体 User.java
package com.eudsk.entity; public class User { private String username; private String password; 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; } }
3. 第三种取值方式 ModelDriven
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="loginlogin" method="post"> <input type="text" name="username"/><br/> <input type="password" name="password"><br/> <input type="submit"> </form> </body> </html>
ModelDrivenAction.java
package com.eudsk.action; import com.eudsk.entity.User; import com.opensymphony.xwork2.ModelDriven; public class ModelDrivenAction implements ModelDriven<User>{ private User user = new User(); public String login() { String state=""; if("j06".equals(user.getUsername()) && "123".equals(user.getPassword())) { state="ok"; }else{ state="error"; } return state; } @Override public User getModel() { return user; } }
ok.jsp, error.jsp, User.java 与上面第一种方式相同。