struts2 参数注入 方法拦截器
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://java.sun.com/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee" id="WebApp_9" version="2.4">
<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>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="crm" namespace="/" extends="struts-default" >
<interceptors>
<interceptor name="myInter" class="com.huawei.interceptor.MyInterceptor" />
<interceptor name="myMethodInter" class="com.huawei.interceptor.MyMethodInterceptor" >
<param name="includeMethods">test1,test3</param>
<!-- <param name="excludeMethods">test2,test4</param> -->
</interceptor>
<interceptor-stack name="myStack">
<!-- 拦截器的执行顺序是根据 interceptor-ref的前后顺序-->
<!-- <interceptor-ref name="defaultStack" /> -->
<!-- <interceptor-ref name="myInter" /> -->
<interceptor-ref name="myMethodInter" />
<!-- <interceptor-ref name="token" /> -->
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack" />
<!-- 全局result -->
<global-results>
<result name="success">/ok.jsp</result>
</global-results>
</package>
<package name="default" namespace="/" extends="crm">
<action name="firstAction" class="com.huawei.s2.action.FirstAction" >
<!-- <result name="invalid.token" >/error.jsp</result> -->
</action>
</package>
</struts>
1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>This is my JSP page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="firstAction">
<input name="uname" value="zhangsan"><br>
<input name="sal" value="10000.0"><br>
<%-- <s:token></s:token> --%>
<input type="submit" value="提交">
</form>
</body>
</html>
MyInterceptor:
package com.huawei.interceptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
* 要么实现 Interceptor 接口 要么 继承类
* @author Administrator
*
*/
public class MyInterceptor extends AbstractInterceptor{
//以下要实现参数注入的功能
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object actionObj =invocation.getAction();//获取action
Class clazz = actionObj.getClass();//action对象对应的反射对象
Map<String, Object> paramsMap = ActionContext.getContext().getParameters();
if(paramsMap!=null&¶msMap.size()>0){
for(String key:paramsMap.keySet()){
Field field = clazz.getDeclaredField(key);
String setterName = "set"+key.substring(0,1).toUpperCase()+key.substring(1);
Method setterMethod = clazz.getDeclaredMethod(setterName, field.getType());
String[] mapValue = (String[]) paramsMap.get(key);
if(field.getType()==Double.class){
setterMethod.invoke(actionObj, Double.parseDouble(mapValue[0]));
}else {
setterMethod.invoke(actionObj, mapValue[0]);
}
}
}
invocation.invoke();
return null;
}
}
MyMethodInterceptor:
package com.huawei.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
* function:方法拦截器
* @author Administrator
*
*/
public class MyMethodInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
System.out.println("方法执行前");
invocation.invoke();
System.out.println("方法执行后");
return null;
}
}
action:
package com.huawei.s2.action;
public class FirstAction {
private String uname;
private Double sal;
public String execute(){
System.out.println(uname+"========FirstAction======="+sal);
return "success";
}
/**
* test1()~test4():是用于验证方法拦截器的
*/
public void test1(){
System.out.println("=======FirstAction =========test1=====");
}
public void test2(){
System.out.println("=======FirstAction =========test2=====");
}
public void test3(){
System.out.println("=======FirstAction =========test3=====");
}
public void test4(){
System.out.println("=======FirstAction =========test4=====");
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
}
struts2 参数注入 方法拦截器的更多相关文章
- Struts2之类范围拦截器和方法拦截器
1.Struts2拦截器的体系结构 Struts2拦截器最大的特点是其透明性,即用户感觉不到它的存在,但我们在使用Struts2框架时,拦截器时时刻刻都在帮助我们处理很多事情. 包括: 文件上传 表单 ...
- 【Java EE 学习 69 上】【struts2】【paramsPrepareParamsStack拦截器栈解决model对象和属性赋值冲突问题】
昨天有同学问我问题,他告诉我他的Action中的一个属性明明提供了get/set方法,但是在方法中却获取不到表单中传递过来的值.代码如下(简化后的代码) public class UserAction ...
- [Abp vNext 源码分析] - 3. 依赖注入与拦截器
一.简要说明 ABP vNext 框架在使用依赖注入服务的时候,是直接使用的微软提供的 Microsoft.Extensions.DependencyInjection 包.这里与原来的 ABP 框架 ...
- 【struts2】预定义拦截器
1)预定义拦截器 Struts2有默认的拦截器配置,也就是说,虽然我们没有主动去配置任何关于拦截器的东西,但是Struts2会使用默认引用的拦截器.由于Struts2的默认拦截器声明和引用都在这个St ...
- 使用struts2中默认的拦截器以及自定义拦截器
转自:http://blog.sina.com.cn/s/blog_82f01d350101echs.html 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Acti ...
- Struts2基础学习(五)—拦截器
一.概述 1.初识拦截器 Interceptor 拦截器类似前面学过的过滤器,是可以在action执行前后执行的代码,是我们做Web开发经常用到的技术.比如:权限控制.日志等.我们也可以将多 ...
- Node.js与Sails~方法拦截器policies
回到目录 policies sails的方法拦截器类似于.net mvc里的Filter,即它可以作用在controller的action上,在服务器响应指定action之前,对这个action进行拦 ...
- Struts2基础-4-2 -struts拦截器实现权限控制案例+ 模型驱动处理请求参数 + Action方法动态调用
1.新建项目,添加jar包到WEB-INF目录下的lib文件夹,并添加到builde path里面 整体目录结构如下 2.新建web.xml,添加struts2核心过滤器,和默认首页 <?xml ...
- 在struts2中配置自定义拦截器放行多个方法
源码: 自定义的拦截器类: //自定义拦截器类:LoginInterceptor ; package com.java.action.interceptor; import javax.servlet ...
随机推荐
- PHP接口开发加密技术实例原理与例子
下面例子简单讲解PHP接口开发加密技术:如app要请求用户列表,api是“index.php?module=user&action=list”app生成token = md5sum (‘use ...
- 用C#实现C/S模式下软件自动在线升级
用C#实现C/S模式下软件自动在线升级 1 前言 长期以来,广大程序员为到底是使用Client/Server,还是使用Browser/Server结构争论不休,在这些争论当中,C/S结构的程序可维护性 ...
- git 不能拉取时,检查是不是被杀毒软件给干掉了
我这儿是 \Git\bin\sh.exe 被干掉了. 添加排除,并从隔离区中还原.
- visual leak detector用法
百度vld和windbg安装 配置symbol路径 配置环境变量 _NT_SYMBOL_PATH SRV*E:\symbols*http://msdl.microsoft.com/download/s ...
- 如何使input双击时不显示历史记录
原文地址:http://highping.iteye.com/blog/359428 HTML的输入框可以拥有自动完成的功能,当你往输入框输入内容的时候,浏览器会从你以前的同名输入框的历史记录中查找出 ...
- MySQL 报错记录
#--------------------------------------------------------------------------------------------------- ...
- 如何在CentOS中添加Swap
1.检查 Swap 空间 在设置 Swap 文件之前,有必要先检查一下系统里有没有既存的 Swap 文件.运行以下命令: 1 swapon -s 如果返回的信息概要是空的,则表示 Swap 文件不存在 ...
- [UE4]GameMode
GameMode定义了正在玩的游戏规则,积分等方面,游戏中有些数据和逻辑不适合放在某一个对象身上,这些数据在整个游戏运行中腰持续存在的(比如:积分.排名). 每次游戏一启动,GameMode就被创建, ...
- Python 编程规范梳理
缘由 由于项目团队中新加入了几名攻城狮, 大家之前的背景各不相同,写出的代码也是“风格迥异”.正所谓:“无规则不成方圆”,因此需要对编程进行必要的规范. 整体的思路是:依照PEP8 Python 编码 ...
- GO ‘N’ Times,SQL执行同一个语句多次
GO (Transact-SQL) 语法 GO [count] 参数 count 为一个正整数. GO 之前的批处理将执行指定的次数. 源文档 <http://msdn.micros ...