struts2整合spring

1、引入spring.jar、struts2-spring-plugin-2.1.8.jar、commons-logging.jar

2、web.xml配置 

  <context-param>

<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

3、使用举例

 3.1、客户端

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<%@ taglib prefix= " s " uri= " /struts-tags " %>
< html >
< head >
< title >获取书籍 </ title >
</ head >
< body >    
     < s:form  action ="getSpringBooks.action" >        
         < s:textfield  name ="userName"  label ="用户名" />
         < s:textfield  name ="passWord"  label ="密码" />                
         < s:submit  value ="登录" />        
     </ s:form >    
</ body >
</ html > 

 3.2、struts.xml配置action

 <!-- 手动装配 

        <action name="getSpringBooks" class="springGetBooksAction">
        
            <result name="login">/Login.jsp</result>
            <result name="success">/books.jsp</result>
        </action>
         -->
         <!--  自动装配  -->
         < action  name ="getSpringBooks"  class ="Action.GetBooksAction" >
        
             < result  name ="login" >/Login.jsp </ result >
             < result  name ="success" >/books.jsp </ result >
         </ action >


3.3、action 

 View Code

 
package Action;

import module.myService;

import com.opensymphony.xwork2.ActionSupport;


public  class GetBooksAction  extends ActionSupport
{
     /**
     * 
     
*/
     private  static  final  long serialVersionUID = -1280978797644723018L;

     private String ttt;
    
     private String[] books;
    
     public  void setBooks( final String[] books)
    {
         this.books=books;
    }
     public String[] getBooks()
    {
         return  this.books;
    }
    
    myService bks;
    
     public myService getBks()
    {
         return bks;
    }
     public  void setBks(myService bks)
    {
         this.bks = bks;
    }
     public String execute()  throws Exception
    {    
         if(bks== null)
        {
             throw  new Exception("myService 没有创建");
             // bks=new bookService();
        }
        
        setBooks(bks.getBooks());
        setTtt("getBooks.Action!");
         return SUCCESS;            
    }
     public  void setTtt( final String ttt) {
         this.ttt = ttt;
    }
     public String getTtt() {
         return ttt;
    } 

 

 

3.4、myService和bookService

 package module;


public  interface myService 
{
     public String[] getBooks();    
}



package module;

public  class bookService  implements myService
{
     private String[] books= new String[]{
            "Spring2.0研究",
            "java精通",
            "jee ajax",
            "Struts,Spring,Hibernate深入研究"
            };
    
     public String[] getBooks()
    {
         return books;
    }    
}

 

3.5、 applicationContext.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:p
="http://www.springframework.org/schema/p"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >

     <!--  手动装配
    <bean id="bookService" class="module.bookService"/>
    
    <bean id="springGetBooksAction" class="Action.GetBooksAction">
        <property name="bks" ref="bookService"/>
    </bean>
    
-->
    
     <!--  自动装配  -->
     < bean  id ="bks"  class ="module.bookService" />
    
    
</ beans >

 

 

 

 

 

 

 

你可能感兴趣的:(struts2)