在上一篇环境搭建:Strtus2.5.26环境搭建(简单例子)我们成功搭建了Struts2.5.26的最新例子,但是我习惯使用注解模式,不喜欢改struts.xml配置文件,并且还习惯使用注解模式的动态方法调用,例如我只需要写一个Action类,然后写很多方法,我就直接可以用http://ip:port/项目/xxxAction!method.action 就可以调用就好了,然后只需要添加方法即可,怎么实现呢,下面举个例子,当然是在上一篇笔记的基础上。
一、在web.xml添加如下参数,指明action所在的包
<init-param><!-- 固定格式 --><param-name>actionPackages</param-name><param-value>com.suibibk.action</param-value></init-param>
添加完后内容如下
<?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>Struts2526</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><filter><filter-name>struts-prepare</filter-name><filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class><init-param><!-- 固定格式 --><param-name>actionPackages</param-name><param-value>com.suibibk.action</param-value></init-param></filter><filter-mapping><filter-name>struts-prepare</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
二、struts.xml配置文件修改
<?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"><!-- 上面的头,注意版本,从样例里复制过来showcase.war\WEB-INF\src\java\struts.xml--><struts><!-- 开启使用开发模式,详细错误提示 --><constant value="false" name="struts.devMode"/><!-- 指定资源编码类型 --><constant value="UTF-8" name="struts.i18n.encoding"/><!-- 指定每次请求到达,重新加载资源文件 --><constant value="false" name="struts.i18n.reload"/><!-- 指定每次配置文件更改后,自动重新加载 --><constant value="false" name="struts.configuration.xml.reload"/><!-- 默认后缀 --><constant value="action" name="struts.action.extension"/><!-- 把主题配置为simple --><constant value="simple" name="struts.ui.theme"/><!-- 开启动态方法调用 --><constant value="true" name="struts.enable.DynamicMethodInvocation"/><!-- 上传文件大小1G --><constant value="1048576000" name="struts.multipart.maxSize"/><!-- 第1步:先定义一个包 --><package name="default" extends="struts-default"><global-allowed-methods>regex:.*</global-allowed-methods></package></struts>
主要是要开启动态方法调用:
<constant value="true" name="struts.enable.DynamicMethodInvocation"/>
以及允许动态方法调用
<global-allowed-methods>regex:.*</global-allowed-methods>
三、用注解模式创建Action类
package com.suibibk.action;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;@ParentPackage(value = "default")@Namespace(value = "/")@Action(value = "indexAction",results = {@Result(name = "test",location = "/test.jsp"),@Result(name = "test2",location = "/test2.jsp")})public class IndexAction{public String test(){System.out.println("我是action,被test1调用");return "test";}public String test2(){System.out.println("我是action,被test2调用");return "test2";}}
ParentPackage对应的是struts.xml中的package.
四、新建test.jsp和test2.jsp页面测试
http://localhost:8080/Struts2526/indexAction!test2.actionhttp://localhost:8080/Struts2526/indexAction!test.action
测试结果OK!
