Struts2的Action访问
● 示例项目结构
● demo1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <h1>Action的访问</h1> <h3>通过method方法</h3> <a href="${pageContext.request.contextPath}/userFind.action">查询用户</a> <a href="${pageContext.request.contextPath}/userUpdate.action">修改用户</a> <a href="${pageContext.request.contextPath}/userDelete.action">删除用户</a> <a href="${pageContext.request.contextPath}/userSave.action">保存用户</a><br/> <h3>通过通配符的方法</h3> <a href="${pageContext.request.contextPath}/product_find.action">查询商品</a> <a href="${pageContext.request.contextPath}/product_update.action">修改商品</a> <a href="${pageContext.request.contextPath}/product_delete.action">删除商品</a> <a href="${pageContext.request.contextPath}/product_sava.action">保存商品</a><br/> <h3>通过动态方法访问</h3> <a href="${pageContext.request.contextPath}/customer!find.action">查询客户</a> <a href="${pageContext.request.contextPath}/customer!update.action">修改客户</a> <a href="${pageContext.request.contextPath}/customer!delete.action">删除客户</a> <a href="${pageContext.request.contextPath}/customer!sava.action">保存客户</a><br/> </body> </html>
● struts2.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> <include file="com/sve/Struts2/demo2/struts_demo2.xml"/> </struts>
● web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>Struts2</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> </web-app>
● struts2_demo2.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="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 通过method方法 --> <action name="userFind" class="com.sve.Struts2.demo2.UserAction" method="find"></action> <action name="userUpdate" class="com.sve.Struts2.demo2.UserAction" method="update"></action> <action name="userDelete" class="com.sve.Struts2.demo2.UserAction" method="delete"></action> <action name="userSave" class="com.sve.Struts2.demo2.UserAction" method="save"></action> <!-- 通配符的方法 --> <action name="*_*" class="com.sve.Struts2.demo2.ProductAction" method="{2}"></action> <!-- 动态方法访问 --> <action name="customer" class="com.sve.Struts2.demo2.CustomerAction"></action> </package> </struts>
● UserAction.java
package com.sve.Struts2.demo2; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ public String find() { System.out.println("查询用户。。。"); return NONE; } public String update() { System.out.println("修改用户。。。"); return NONE; } public String delete() { System.out.println("删除用户。。。"); return NONE; } public String save() { System.out.println("保存用户。。。"); return NONE; } }
● CustomerAction.java
package com.sve.Struts2.demo2; import com.opensymphony.xwork2.ActionSupport; public class CustomerAction extends ActionSupport { public String find() { System.out.println("查询客户。。。"); return NONE; } public String update() { System.out.println("修改客户。。。"); return NONE; } public String delete() { System.out.println("删除客户。。。"); return NONE; } public String sava() { System.out.println("保存客户。。。"); return NONE; } }
● ProductAction.java
package com.sve.Struts2.demo2; import com.opensymphony.xwork2.ActionSupport; public class ProductAction extends ActionSupport { public String find() { System.out.println("查询商品。。。"); return NONE; } public String update() { System.out.println("修改商品。。。"); return NONE; } public String delete() { System.out.println("删除商品。。。"); return NONE; } public String sava() { System.out.println("保存商品。。。"); return NONE; } }
1.method方法
<action name="userFind" class="com.sve.Struts2.demo2.UserAction" method="find"></action>
在struts2_demo2.xml中,通过设置action中的method的值,值为class对应的方法名
2.通配符的方法(常用的)
<action name="*_*" class="com.sve.Struts2.demo2.ProductAction" method="{2}"></action>
与method方法略微不同,method的值设置为name值对应星编号,以上也可设置为
<action name="product_*" class="com.sve.Struts2.demo2.ProductAction" method="{1}"></action>
3.动态方法
使用动态方法前必须先设置constant,允许动态方法的使用
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
无须设置method,动态方法是修改访问链接,如示例中的
<action name="customer" class="com.sve.Struts2.demo2.CustomerAction"></action>
<a href="${pageContext.request.contextPath}/customer!find.action">查询客户</a>
customer为action中设置的name值,然后加入“!”,后加上想使用的方法名