Spring整合Struts2中拦截链与注解的使用
一、spring与struts2集成环境的搭建
(1)引入jar包
a.引入struts2(struts-2.2.3)相关jar包
1.commons-fileupload-1.2.2.jar
2.commons-io-2.0.1.jar
3.commons-lang-2.5.jar
4.freemarker-2.3.16.jar
5.javassist-3.11.0.GA.jar
6.ognl-3.0.1.jar
7.struts2-core-2.2.3.jar
8.struts2-spring-plugin-2.2.3.jar
9.xwork-core-2.2.3.jar
b.引入Spring(spring-framework-2.5.6)相关jar包
1.aspectjrt.jar
2.aspectjweaver.jar
3.cglib-nodep-2.1_3.jar
4.common-annotations.jar
5.commons-dbcp.jar
6.commons-logging-1.1.1.jar
7.commons-pool.jar
8.spring.jar
(2)配置web.xml文件。这里需要注意的事情是spring的配置文件application.xml文件如果不做相应的路径配置,tomcat会默认在WEB-INF下进行加载,但是一般情况我们会将所有的配置文件放到一个统一的文件夹中进行管理,所以以一下web.xml配置为例,我们将所有的xml配置文件放在了src下的config文件夹中,所以配置文件中才出现了上下文配置标签(<param-name>contextConfigLocation</param-name>项)以及<param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>项,这些都是为了制定配置文件的加载路径:
[html]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>InterceptorTest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
二、拦截链的使用。拦截链使用中所要注意的问题是无论是单独使用拦截器还是使用拦截链,一定要在拦截器(或拦截链)的后面显示的调用默认拦截器(或拦截链)。以使用拦截链为例struts.xml文件中的内容为:
[html]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"https://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.action.extension" value=","></constant>
<package name="test" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="checkInterceptor" class="checkInterceptor"></interceptor>
<interceptor-stack name="myInterceptor">
<interceptor-ref name="checkInterceptor"></interceptor-ref>
<!-- 一定要调用默认的拦截链 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 用于登录 -->
<action name="login" class="loginAction">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
<result name="checkSession">/checkSession.jsp</result>
<interceptor-ref name="myInterceptor"></interceptor-ref>
</action>
<!-- 清除session -->
<action name="delSession" class="delSession">
<result name="back">/login.jsp</result>
</action>
</package>
</struts>
三、注解的使用。注解同样提供了多态机制,它更类似与接口功能的拓展。通过对配置文件中具体实现类进行配置达到灵活更改的目的。注解的使用同样遵循相同的映射规律,即使用注解的类的变量名称一定要与配置文件中的bean name相同,并且首字母小写。具体如下:
[java]
package com.action;
import java.util.Map;
import javax.annotation.Resource;
import com.common.CheckIsNull;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.user.User;
public class LoginAction extends ActionSupport {
//此处为注解的使用处
@Resource
private CheckIsNull checkIsNull;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String execute() throws Exception {
boolean flag = checkIsNull.IsNull(user.getUsername());
if(flag){
if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){
Map session = (Map)ActionContext.getContext().get("session");
session.put("username", user.getUsername());
return "success";
}else{
return "error";
}
}else{
System.out.println("用户名和密码都不能为空");
return "error";
}
}
}
applicationContextbean.xml配置文件:
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd
https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.5.xsd
https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-2.5.xsd
https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >
<!-- Spring管理Struts2的Action -->
<bean name="loginAction" class="com.action.LoginAction" scope="prototype"></bean>
<bean name="checkInterceptor" class="com.interceptor.MyIntercepor"></bean>
<!-- 此处为应用注解的映射方式 -->
<bean name="checkIsNull" class="com.common.CheckPassword"></bean>
<bean name="delSession" class="com.action.DelSession"></bean>
</beans>
以上为在使用Spring集成struts2中拦截链和注解的使用以及需要注意的地方。总的来讲spring在其中所起的主要作用就是通过映射机制完成注入。
上一篇: vuejs的初步使用