个人随笔
目录
环境搭建:Strtus2.5.26动态方法调用注解模式环境搭建
2020-12-16 15:34:01

在上一篇环境搭建:Strtus2.5.26环境搭建(简单例子)我们成功搭建了Struts2.5.26的最新例子,但是我习惯使用注解模式,不喜欢改struts.xml配置文件,并且还习惯使用注解模式的动态方法调用,例如我只需要写一个Action类,然后写很多方法,我就直接可以用
http://ip:port/项目/xxxAction!method.action 就可以调用就好了,然后只需要添加方法即可,怎么实现呢,下面举个例子,当然是在上一篇笔记的基础上。

一、在web.xml添加如下参数,指明action所在的包

  1. <init-param>
  2. <!-- 固定格式 -->
  3. <param-name>actionPackages</param-name>
  4. <param-value>com.suibibk.action</param-value>
  5. </init-param>

添加完后内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>Struts2526</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <filter>
  8. <filter-name>struts-prepare</filter-name>
  9. <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  10. <init-param>
  11. <!-- 固定格式 -->
  12. <param-name>actionPackages</param-name>
  13. <param-value>com.suibibk.action</param-value>
  14. </init-param>
  15. </filter>
  16. <filter-mapping>
  17. <filter-name>struts-prepare</filter-name>
  18. <url-pattern>/*</url-pattern>
  19. </filter-mapping>
  20. </web-app>

二、struts.xml配置文件修改

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  4. "http://struts.apache.org/dtds/struts-2.5.dtd">
  5. <!-- 上面的头,注意版本,从样例里复制过来
  6. showcase.war\WEB-INF\src\java\struts.xml
  7. -->
  8. <struts>
  9. <!-- 开启使用开发模式,详细错误提示 -->
  10. <constant value="false" name="struts.devMode"/>
  11. <!-- 指定资源编码类型 -->
  12. <constant value="UTF-8" name="struts.i18n.encoding"/>
  13. <!-- 指定每次请求到达,重新加载资源文件 -->
  14. <constant value="false" name="struts.i18n.reload"/>
  15. <!-- 指定每次配置文件更改后,自动重新加载 -->
  16. <constant value="false" name="struts.configuration.xml.reload"/>
  17. <!-- 默认后缀 -->
  18. <constant value="action" name="struts.action.extension"/>
  19. <!-- 把主题配置为simple -->
  20. <constant value="simple" name="struts.ui.theme"/>
  21. <!-- 开启动态方法调用 -->
  22. <constant value="true" name="struts.enable.DynamicMethodInvocation"/>
  23. <!-- 上传文件大小1G -->
  24. <constant value="1048576000" name="struts.multipart.maxSize"/>
  25. <!-- 第1步:先定义一个包 -->
  26. <package name="default" extends="struts-default">
  27. <global-allowed-methods>regex:.*</global-allowed-methods>
  28. </package>
  29. </struts>

主要是要开启动态方法调用:

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

以及允许动态方法调用

  1. <global-allowed-methods>regex:.*</global-allowed-methods>

三、用注解模式创建Action类

  1. package com.suibibk.action;
  2. import org.apache.struts2.convention.annotation.Action;
  3. import org.apache.struts2.convention.annotation.Namespace;
  4. import org.apache.struts2.convention.annotation.ParentPackage;
  5. import org.apache.struts2.convention.annotation.Result;
  6. @ParentPackage(value = "default")
  7. @Namespace(value = "/")
  8. @Action(value = "indexAction",results = {
  9. @Result(name = "test",location = "/test.jsp"),
  10. @Result(name = "test2",location = "/test2.jsp")
  11. })
  12. public class IndexAction{
  13. public String test(){
  14. System.out.println("我是action,被test1调用");
  15. return "test";
  16. }
  17. public String test2(){
  18. System.out.println("我是action,被test2调用");
  19. return "test2";
  20. }
  21. }

ParentPackage对应的是struts.xml中的package.

四、新建test.jsp和test2.jsp页面测试

  1. http://localhost:8080/Struts2526/indexAction!test2.action
  2. http://localhost:8080/Struts2526/indexAction!test.action

测试结果OK!

 639

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2