1、概述
2、resultd的name属性
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> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> <!-- 取名与用户模块相对应 --> <!-- <package name="default" namespace="/" extends="struts-default"> --> <package name="user" namespace="/user" extends="struts-default"> <default-action-ref name="index"/> <action name="index"> <result>/index.jsp</result> </action> <action name="login" class="com.ljb.web.action.LoginAction"> <result> /loginSuccess.jsp </result> </action> </package> </struts>
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="user/login" method="post"> 用户名:<input type="text" name="userName"/><br/> 密码:<input type="password" name="password"/><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form> </body> </html>
注:路径(user/login)
小结:
3、result的type属性
<?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> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant> <package name="user" namespace="/user" extends="struts-default"> <default-action-ref name="index"/> <action name="index"> <result>/index.jsp</result> </action> <action name="login" class="com.ljb.web.action.LoginAction" method="login"> <result name="success"> /loginSuccess.jsp </result> <result name="input"> /login.jsp </result> </action> </package> </struts>
package com.ljb.web.action; import com.opensymphony.xwork2.ActionSupport; /** * 第一种传递参数方法(设置私有属性,设置set与get方法) * @author Administrator * */ public class LoginAction extends ActionSupport { private String userName; private String password; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } public String login () { if (userName.equals("admin")&&password.equals("ok")) { return SUCCESS; } else { return INPUT; } } }
小结:
4、全局结果
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> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <package name="house" namespace="/house" extends="struts-default"> <global-results> <result name="error">/error.jsp</result> </global-results> <!-- <action name="house_add" class="com.ljb.web.action.HouseAction" method="add"> <result>/house_add_success.jsp</result> </action> <action name="house_update" class="com.ljb.web.action.HouseAction" method="update"> <result>/house_update_success.jsp</result> </action> --> <action name="house_*" class="com.ljb.web.action.HouseAction" method="{1}"> <result>/house_{1}_success.jsp</result> </action> </package> </struts>
house_add.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="house/house_add" method="post"> <input type="submit" value="添加房屋信息"/> </form> </body> </html>
HouseAction类:
package com.ljb.web.action; import com.opensymphony.xwork2.ActionSupport; public class HouseAction extends ActionSupport { /** * 添加房屋信息 * @return */ public String add () { System.out.println("处理添加房屋信息。"); // 模拟添加房屋信息异常 try { if (1==1) { throw new Exception(); } } catch (Exception e) { return ERROR; } return SUCCESS; } /** * 修改房屋信息 * @return */ public String update () { System.out.println("处理修改房屋信息。"); return SUCCESS; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
5、动态结果
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="user/login" method="post"> 用户名:<input type="text" name="userName"/><br/> 密码:<input type="password" name="password"/><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form> </body> </html>
commonSuccess.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> <h1>普通用户页面</h1> </body> </html>
adminSuccess.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> <h1>管理员页面</h1> </body> </html>
CommonAction:
package com.ljb.web.action; import com.opensymphony.xwork2.ActionSupport; public class CommonAction extends ActionSupport { @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
AdminAction:
package com.ljb.web.action; import com.opensymphony.xwork2.ActionSupport; public class AdminAction extends ActionSupport { @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
LoginAction:
package com.ljb.web.action; import com.opensymphony.xwork2.ActionSupport; /** * 第一种传递参数方法(设置私有属性,设置set与get方法) * @author Administrator * */ public class LoginAction extends ActionSupport { // 定义下一个需要跳转的action对应的字符串名称 private String nextAction; public String getNextAction() { return nextAction; } public void setNextAction(String nextAction) { this.nextAction = nextAction; } private String userName; private String password; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } public String login () { if (userName.equals("admin")&&password.equals("admin")) { nextAction = "admin"; return SUCCESS; } else if (userName.equals("common") && password.equals("common")) { nextAction = "common"; return SUCCESS; } else { return INPUT; } } }
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> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <package name="user" namespace="/user" extends="struts-default"> <default-action-ref name="index"/> <action name="index"> <result>/index.jsp</result> </action> <action name="login" class="com.ljb.web.action.LoginAction" method="login"> <result name="success" type="redirectAction"> ${nextAction} </result> <result name="input"> /login.jsp </result> </action> <action name="common" class="com.ljb.web.action.CommonAction"> <result>/commonSuccess.jsp</result> </action> <action name="admin" class="com.ljb.web.action.AdminAction"> <result>/adminSuccess.jsp</result> </action> </package> </struts>
6、小结