Spring整合Struts2(1)

来源:互联网 发布:中国m1m2数据最新 编辑:程序博客网 时间:2024/06/06 07:16

 Spring整合Struts2的步骤
1、先配置运行一个Struts2工程。
 * 拷贝相关jar包到lib目录下
 * 拷贝struts.xml到src目录下
 * 在web.xml中加入Struts2的拦截器
  <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>
2、配置Spring:
 * 相关jar包拷贝到lib目录下。
 * 在src目录下创建Spring的配置文件:bean.xml
 * 在web.xml文件中加入ContextLoaderListener,用来创建Spring容器:
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:bean.xml</param-value>
     </context-param>
    
     <!-- 使用ContextLoaderListener初始化Spring容器 -->
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener
   </listener-class>
  </listener>
3、Struts2和Spring的整合
 * 把struts2-spring-plugin-2.2.3.1.jar拷贝到lib路径下
 * 把Struts2的Action的创建交给Spring
 * 在Struts2的配置文件中配置Action时,class属性指定为Spring中生成的Action的bean ID。
 注意:必须配置Struts2的Action的scope为prototype  

 

 

 

具体的实例如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="login">
  用户名:<input type="text" name="username"> <br/>
  密   码:<input type="text" name="password"> <br/>
  <input type="submit" value="提交">   <input type="reset" value="重置"> <br/>
 </form>

</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="login.jsp">登陆页面</a> <br/>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆成功!<br/>
你的用户名是${username },密码是${password }.
</body>
</html>

 

 

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/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ss</display-name>
 
  <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>
   
    <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:bean.xml</param-value>
    </context-param>
   
    <!-- 使用ContextLoaderListener初始化Spring容器 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

package cn.hnpi.lis.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
 
 private String username;
 private String password;
 
 public String login(){
  if(username == null || password == null ||
    username.trim().equals("") || password.trim().equals("")){
   return LOGIN;
  }else{
   return SUCCESS;
  }
 }
 
 
 
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 

}

 

 

bean.xml

<?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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
 
 <!-- 由Spring生成Struts2的Action -->
 <bean id="loginAction" class="cn.hnpi.lis.action.LoginAction" scope="prototype"></bean>
 
</beans>

 

<?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>

    <constant name="struts.devMode" value="false" />
    <constant name="struts.i18n.encoding" value="UTF-8" />

    <package name="default" namespace="/" extends="struts-default">
  <!--  没有Spring时的配置方式
  <action name="login" class="cn.hnpi.lis.action.LoginAction" method="login">
   <result name="success">login_success.jsp</result>
   <result name="login">login.jsp</result>
  </action>
        -->
       
        <!-- 由Spring创建Action -->
        <action name="login" class="loginAction" method="login">
   <result name="success">login_success.jsp</result>
   <result name="login">login.jsp</result>
  </action>
    </package>


</struts>

 

 

 

原创粉丝点击