Struts2之三种动态方法的调用

本文详细介绍了Struts2框架中动态方法调用的三种方式:指定method属性、感叹号方式和通配符方法。通过具体示例,讲解了每种方法的配置与实现过程,帮助读者深入理解Struts2动态方法调用机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本项目端口号为80

jsp文件

HelloWorld.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 charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	This is HelloWorld.jsp
</body>
</html>

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 charset="UTF-8">
<title>add</title>
</head>
<body>
	This is add
</body>
</html>

update.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 charset="UTF-8">
<title>update</title>
</head>
<body>
	This is update
</body>
</html>

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" 
	metadata-complete="false" version="3.1">
  <absolute-ordering/>
  <display-name>WebXml</display-name>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<!--  注意你的配置,2.5版本直接是filter,不是ng.filter   -->
	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <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>
</web-app>

struts.xml

第一种方法:指定method属性

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
   "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="default" namespace="/" extends="struts-default">
    <!-- 动态方法调用第一种 指定method属性 -->
    <action name="helloworld" 
            class="com.chen.action.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
      <action name="addAction" 
            class="com.chen.action.HelloWorldAction" 
            method="add">
            <result name="success">/add.jsp</result>
      </action>
      <action name="updateAction" 
            class="com.chen.action.HelloWorldAction" 
            method="update">
            <result name="success">/update.jsp</result>
      </action> 
   </package>
</struts>

第二种方法:感叹号方式 不推荐

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
   "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="default" namespace="/" extends="struts-default">
    	<!-- 动态方法调用第二种 感叹号方式 不推荐-->
		<!-- struts2.5 为了增加安全性,在 struts.xml 添加了这么个属性:regex:.* -->
		<global-allowed-methods>regex:.*</global-allowed-methods>
		<action name="helloworld" 
            class="com.chen.action.HelloWorldAction" 
            method="execute">
        	<result name="success">/HelloWorld.jsp</result>
            <result name="add">/add.jsp</result>
            <result name="update">/update.jsp</result>
     	</action>
     </package>
     <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
     <!-- 在浏览器打开http://localhost/HelloWorld/helloworld.action 或者在helloworld后面加上!add或者!update-->
</struts>

第三种方法:通配符方法 推荐

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
   "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="default" namespace="/" extends="struts-default">
    <!-- 动态方法调用第三种 通配符方式  推荐--> 
    <!-- struts2.5 为了增加安全性,在 struts.xml 添加了这么个属性:regex:.* -->
    <!-- 通配符方式一: -->
    <global-allowed-methods>regex:.*</global-allowed-methods> 
    <action name="helloworld_*" 
            class="com.chen.action.HelloWorldAction" 
            method="{1}">
            <!-- 在浏览器打开http://localhost/HelloWorld/helloworld.action 或者在helloworld后面加上_add或者_update -->
            <result name="success">/HelloWorld.jsp</result>
            <result name="add">/{1}.jsp</result>
            <result name="update">/{1}.jsp</result>
      </action>
      <!-- 通配符方式二:name属性中如果有多个通配符的写法:name="helloworld_*_*" 并且method="{1}{2}  -->
            
      <!-- 通配符方式三:
      <action name="*_*" 
            class="com.chen.action.{1}Action" 
            method="{2}">
            http://localhost/_200128StrutsHello/HelloWorld.action  类名除去Action后的字段后面加上_add或者_update
            <result name="success">/HelloWorld.jsp</result>
            <result name="add">/{2}.jsp</result>
            <result name="update">/{2}.jsp</result>
      </action> -->
	</package>
</struts>

java文件

package com.chen.action;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport 
								implements ServletRequestAware,ServletResponseAware,ServletContextAware{
	private HttpServletRequest request;
	
	public String add() {
		//return SUCCESS;
		return "add";
	}

	public String update() {
		//return SUCCESS;
		return "update";
	}
	@Override
	public String execute() throws Exception {
		System.out.println("执行成功");
		return SUCCESS;
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值