关于Struts2 使用! 感叹号 动态调用Action中的方法

  • 首先添加一个UserAction:
    public class UserAction extends ActionSupport {
    	private static final long serialVersionUID = 1L;
    	private String info;
    
    	public String getInfo() {
    		return info;
    	}
    
    	public void setInfo(String info) {
    		this.info = info;
    	}
    
    	public String add() {
    		info = "添加";
    		return "addInfo";
    	}
    
    	public String update() {
    		info = "修改";
    		return "updateInfo";
    	}
    }

    里提供了两个方法:add和update,同时 add方法返回addInfo,update方法返回upateInfo。

  • 下面是struts.xml配置文件。该文件必须放在src的目录下!必须!

    <?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="myPackage" extends="struts-default">
    	    <!-- 定义action name:访问时此Action的URL class:此Action的类名 -->
    		<action name="userAction" class="com.xx.action.UserAction">
    		    <!-- 当返回结果为 addInfo时(返回结果为方法的返回结果),跳到add.jsp -->
    			<result name="addInfo">add.jsp</result>
    			<!-- 当返回结果为 updateInfo时,跳到add.jsp -->
    			<result name="updateInfo">update.jsp</result>
    		</action>
    	</package>
    </struts>

     

  • 当然web.xml中也需要配置struts的Filter过滤器
    <?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"
    	id="WebApp_ID" version="3.1">
    	<display-name>Struts2-02</display-name>
    	<welcome-file-list>
    		<welcome-file>index.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>
     

上述三步完成后,就可以建立访问连接了。在index.jsp里调用UserAction的add和update方法。

<a href="userAction!add">添加</a><br />
<a href="userAction!update">修改</a><br />

userAction为struts.xml文件中配置的Action节点的name属性。使用!分隔Action请求和请求的字符串,而请求的字符串必须和UserAction中的方法名相同!

此时基本完成,可以通过点击 添加 这个超链接访问action的add方法,并且跳转到add.jsp中!

 

但是,这里缺出现了一个小插曲:

警告: Could not find action or result: /Test01/userAction!add
There is no Action mapped for namespace [/] and action name [userAction!add] associated with context path [/Test01]. - [unknown location]

出现这样的错误。

出现这个的错误,看官方说明:是要在struts.xml中加一句:

<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 

感叹号方式(需要开启),用这种方式需要先开启一个开关。

注:官网还说不推荐使用这种方式,建议大家不要使用~o(∩_∩)o 

转载于:https://my.oschina.net/yuekunge/blog/693159

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值