一、概述html
二、使用method屬性java
house_add.jsp:web
<%@ 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_add" method="post"> <input type="submit" value="添加房屋信息"/> </form> </body> </html>
house_add_success.jsp:apache
<%@ 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> 添加房屋信息成功。 </body> </html>
HouseAction類:jsp
papackage com.ljb.web.action; import com.opensymphony.xwork2.ActionSupport; public class HouseAction extends ActionSupport { /** * 添加房屋信息 * @return */ public String add () { System.out.println("處理添加房屋信息。"); return SUCCESS; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
struts.xml:ide
<?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.i18n.encoding" value="utf-8"></constant> --> <package name="default" namespace="/" extends="struts-default"> <action name="house_add" class="com.ljb.web.action.HouseAction" method="add"> <result>/house_add_success.jsp</result> </action> </package> </struts>
小結:post
三、動態方法調用ui
house_add.jspspa
<%@ 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!add" method="post"> <input type="submit" value="添加房屋信息"/> </form> </body> </html>
HouseAction類:code
package com.ljb.web.action; import com.opensymphony.xwork2.ActionSupport; public class HouseAction extends ActionSupport { /** * 添加房屋信息 * @return */ public String add () { System.out.println("處理添加房屋信息。"); return SUCCESS; } /** * 修改房屋信息 * @return */ public String update () { System.out.println("處理修改房屋信息。"); return SUCCESS; } @Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } }
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="true"></constant> <action name="house" class="com.ljb.web.action.HouseAction" > <result>/house_add_success.jsp</result> </action> </package> </struts>
小結:
四、使用通配符
<?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" /> <action name="house_*" class="com.ljb.web.action.HouseAction" method="{1}"> <result>/house_{1}_success.jsp</result> </action> </package> </struts>
注:其他都不變
五、默認action
<?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="default" namespace="/" extends="struts-default"> <default-action-ref name="index"/> <action name="index"> <result>/index.jsp</result> </action> <action name="house_*" class="com.ljb.web.action.HouseAction" method="{1}"> <result>/house_{1}_success.jsp</result> </action> </package> </struts>
六、匹配順序
小結: