一、概述html
二、resultd的name屬性java
struts.xml:web
<?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.jspapache
<%@ 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)jsp
小結:ide
三、result的type屬性post
<?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; } } }
小結:ui
四、全局結果this
struts.xml:spa
<?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; } }
五、動態結果
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>
六、小結