1,首先引入struts2注解的jar包:struts2-convention-plugin.jar
- <!-- struts2注解 -->
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-convention-plugin</artifactId>
- <version>${struts2-version}</version>
- lt;/dependency>
2,classpath下struts.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.devMode" value="true" /> -->
- <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好 关闭 -->
- <constant name="struts.serve.static.browserCache" value="false" />
- <!-- 当 struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生 产环境下使用),开发阶段最好打开 -->
- <constant name="struts.configuration.xml.reload" value="true"/>
- <!-- 请求参数的编码方式-->
- <constant name="struts.i18n.encoding" value="UTF-8"/>
- <!-- struts2拦截器默认拦截的后缀名 多个可以用逗号隔开 用于做网站伪静态化-->
- <constant name="struts.action.extension" value="html"/>
- <!-- 简单主题 -->
- <constant name="struts.ui.theme" value="simple" />
- <!-- 配置文件上传的总大小 -->
- <constant name="struts.multipart.maxSize" value="2097152000"></constant>
- <!-- 把struts的请求委托给spring管理, 作用:创建Action实例的过程由spring处理,其他的还是由struts2自己处理 -->
- <constant name="struts.objectFactory" value="spring" />
- <!-- 是否开启动态方法调用-->
- <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
- <!-- Struts2约定限制结果页面必须放到/WEB-INF/content/目录中,可以通过下面常量来修改这个限制。-->
- <constant name="struts.convention.result.path" value="/WEB-INF/jsp" />
- <!-- 约定Action包位置:可以使用的Action的祖包,默认为没有, 设置为 cn.javass.ssh 就可以读取这个包及其子包的所有头上注册了【 @Action 】 的类。-->
- <!-- <constant name="struts.convention.action.packages" value="cn.hl.s2sh.user.controller" /> -->
- <!-- 默认配置包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。通过设置struts.convention.package.locators属性来修改这个配置。 -->
- <constant name="struts.convention.package.locators" value="action,actions,struts,struts2,controller" />
- <!-- 约定Action类名:要求Action的命名必须以Action为后缀,可以使用下面常量来修改后缀限制 -->
- <constant name="struts.convention.action.suffix" value="Action" />
- <!-- 配置全局的包 继承自json-default,json-default继承自struts-default包 --->
- <package name="mypackage" extends="json-default">
- <!-- <interceptors>
- 注册自定义登录拦截器
- <interceptor name="login" class="com.hl.zoneSystem_v01.struts.common.MyInterceptor" />
- 自定义拦截器栈
- <interceptor-stack name="myInterceptor">
- <interceptor-ref name="login" />
- <interceptor-ref name="defaultStack">
- <param name="modelDriven.refreshModelBeforeResult">true</param>
- </interceptor-ref>
- </interceptor-stack>
- </interceptors> -->
- <!-- 定义默认栈 -->
- <!-- <default-interceptor-ref name="myInterceptor" /> -->
- <!-- 定义默认action -->
- <!-- <default-action-ref name="notFound" /> -->
- <!-- 全局结果集 -->
- <global-results>
- <!-- ajax请求的结果集 表单验证用,值为true/false-->
- <result type="json" name="ajax">
- <param name="root">message</param>
- </result>
- <!-- ajax请求的结果集 消息推送用,值为数字-->
- <result type="json" name="ajax2">
- <param name="root">message2</param>
- </result>
- <!-- ajax请求的结果集 页面数据传送 返回map-->
- <result type="json" name="jsonResult">
- <param name="root">jsonMap</param>
- </result>
- <!-- 下载 -->
- <result name="downloadFiles" type="stream">
- <!-- 对应web中下载配置:下载类型无限制 文件编码UTF-8-->
- <param name="contentType">application/octet-stream;charset=UTF-8</param>
- <param name="inputName">inputStreamFile</param>
- <!-- 对应web中下载配置:response.setHeader("Content-Disposition", "attachment;filename="+newUser.getFilename()); -->
- <param name="contentDisposition">attachment;filename=${fileName}</param>
- <!-- 对应web中下载配置:byte[] b = new byte[1024]; -->
- <param name="bufferSize">1024</param>
- </result>
- <!-- 项目根页面 -->
- <result name="index" type="redirect">/index.jsp</result>
- <!-- login 登录 -->
- <result name="loginUI">/login.jsp</result>
- <!-- 前台全局消息显示页面 -->
- <result name="clientMessage">/message.jsp</result>
- <!-- action错误,处理结果页面 -->
- <result name="error">/error.jsp</result>
- </global-results>
- <!-- 全局异常 -->
- <!-- <global-exception-mappings>
- <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
- </global-exception-mappings> -->
- </package>
- </struts>
3,action中写法
controller包下
- package cn.hl.s2sh.user.controller;
- import net.sf.ehcache.Cache;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.struts2.convention.annotation.Action;
- import org.apache.struts2.convention.annotation.ExceptionMapping;
- import org.apache.struts2.convention.annotation.ExceptionMappings;
- import org.apache.struts2.convention.annotation.Namespace;
- import org.apache.struts2.convention.annotation.ParentPackage;
- import org.apache.struts2.convention.annotation.Result;
- import org.apache.struts2.convention.annotation.Results;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Controller;
- import cn.hl.s2sh.base.controller.struts2.BaseAction;
- import cn.hl.s2sh.base.plugins.cache.CachedManager;
- /**
- * @Description: user控制器
- * @author hl
- * @date 2014-11-18 下午11:10:21
- *
- */
- @Controller
- @Scope("prototype")
- @ParentPackage("mypackage")
- @Results({
- @Result(name="test1",location="test.jsp"),
- @Result(name="test2",location="/WEB-INF/jsp/test2.jsp"),
- @Result(name="main_postTalk",type="redirectAction",location="main_mindex",params={"uid","${uid}"})
- })
- @ExceptionMappings({@ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
- @Namespace(value="/u")
- public class UserAction extends BaseAction{
- //log4j
- private static final Log log = LogFactory.getLog(UserAction.class);
- @Action("/test1")
- public String test1(){
- CachedManager.putObjectCache("phoneCode", "a", "aaa");
- Cache c = CachedManager.getCache("phoneCode");
- Cache c2 = CachedManager.getCache("phoneCode2");
- System.out.println("--------test1---1");
- //log.debug("--manager-" + manager);
- return "test1";
- }
- @Action(value="test2")
- public String test2(){
- String s = (String) CachedManager.getObjectCache("phoneCode", "a");
- System.out.println(s);
- CachedManager.remove("phoneCode", "a");
- System.out.println("--------test2---2");
- return "test2";
- }
- }
部署启动tomcat,访问
localhost:8080/项目名/u/test1.html
localhost:8080/项目名/u/test2.html
localhost:8080/项目名/test1.html