• spring与struts2的整合


    工程文件结构图:

    1,创建名为loginSpringStruts2的web工程,具体机构如上图,在该工程的/WebRoot/WEB-INF/lib路径下添加struts2常用的JAR包(参见“struts2配置”),同时添加名为spring.jar,  commons-loggin.jar(已经添加),cglib-nodep-2.1_3.jar(AOP用到),struts2-spring-plugin-2.0.11.jar等4个Spring运行库文件。

    2,创建前台视图login.jsp  success.jsp  error.jsp,  具体参见(“struts2配置”

    3,在src下创建applicationContext.xml,配置如下:

    View Code
    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
        
        <!-- 配置Struts2里的Action对象 -->
        <bean id="login" class="action.LoginAction"></bean>
    </beans>

    4,在src下创建struts.properties消息文件,添加“struts.objectFactory=spring"语句,使得Struts2类的生成交给spring完成,具体代码:

    View Code
    struts.objectFactory=spring
    struts.action.extension=action
    struts.locale=en_GB

    5,在src下创建struts.xml, 具体配置如下:

    View Code
    <?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="main" extends="struts-default">
        <action name="login" class="action.LoginAction">
            <result name="error">/error.jsp</result>
            <result name="success">/success.jsp</result>
        </action>
    </package>
    
    </struts>

      相应的action.LoginAction代码如下:

    View Code
    package action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport {
        private String userName;
        private String userPsw;
        
    
        public String getUserName() {
            return userName;
        }
    
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
    
        public String getUserPsw() {
            return userPsw;
        }
    
    
        public void setUserPsw(String userPsw) {
            this.userPsw = userPsw;
        }
    
    
        /**
         * @return
         */
        public String execute() {
            if(getUserName().equals("123")&& getUserPsw().equals("123")){
                return SUCCESS;
            }
    
            
            else return ERROR;
        }
    }

      相应的filter.CharacterEncodingFilter.java的代码如下:

    View Code
    package filter;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class CharacterEncodingFilter implements Filter {
        private String characterEncoding;//编码方式在web.xml中
        private boolean enabled;//是否启动filter,初始值在web.xml中
        
        @Override
        public void init(FilterConfig config) throws ServletException {
            // TODO Auto-generated method stub
            //初始化时加载参数
            //从配置文件中读取设置到编码方式
            characterEncoding=config.getInitParameter("characterEncoding");
            //启动该过滤器完成编码方式到修改
            enabled="true".equalsIgnoreCase(characterEncoding.trim())
                ||"1".equalsIgnoreCase(characterEncoding.trim());
            
        }
        
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            // TODO Auto-generated method stub
            if(enabled||characterEncoding!=null){
                request.setCharacterEncoding(characterEncoding);//设置request编码方式
                response.setCharacterEncoding(characterEncoding);//设置response编码方式
            }
            chain.doFilter(request, response);//doFilter将修改好的request和response参数向下传递;
            
        }
    
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
            characterEncoding=null;
            
        }
    
        
    
    
    
    }

    6,配置web.xml,包括struts2监听,字符集过滤,spring的web监听,并通过<context-param>标签来配置applicationContext.xml文件的位置

    View Code
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- struts2过滤器 -->
        <filter>
          <filter-name>struts</filter-name>
          <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
          <init-param>
              <param-name>struts.action.extension</param-name>
              <param-value>action</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>struts</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <!-- 字符集过滤器 -->
      <filter>
          <filter-name>characterEncodingFilter</filter-name>
          <filter-class>filter.CharacterEncodingFilter</filter-class>
          <init-param>
              <param-name>characterEncoding</param-name>
              <param-value>UTF-8</param-value>
          </init-param>
          <init-param>
              <param-name>enable</param-name>
              <param-value>true</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>characterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <!-- spring的监听 -->
      <listener>
          <listener-class>
              org.springframework.web.context.ContextLoaderListener
          </listener-class>
      </listener>
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
      </context-param>
      
      
    </web-app>
  • 相关阅读:
    java web 工程更改名字
    [转]Eclipse下开发Struts奇怪异常:org.apache.struts.taglib.bean.CookieTei
    【转】myeclipse 自定义视图Customize Perspective 没有反应
    latex建立参考文献的超链接
    latex 脚注编号也成为超链接
    自定义标签TLD文件中,rtexprvalue子标签的意思
    设计模式观察者
    设计模式模板方法
    设计模式策略
    设计模式享元
  • 原文地址:https://www.cnblogs.com/lpshou/p/2792168.html
Copyright © 2020-2023  润新知