Struts2+Spring3 基于注解的配置

jopen 10年前發布 | 56K 次閱讀 Struts2 Spring3 Web框架

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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Struts2Test</display-name>
    <!--  -->
<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>
     <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>

    <!-- 配置歡迎頁 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>




applicationContext.xml  放在WEB-INF目錄下

<?xml version="1.0" encoding="UTF-8"?>
<!--DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"    "http://www.springframework.org/dtd/spring-beans-2.0.dtd"-->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">



 <!-- 使用 annotation 自動注冊bean,并檢查@Controller, @Service, @Repository注解已被注入,也可以分開注釋,或者固定某個目錄下 -->
 <context:component-scan base-package="*" />

</beans>



struts2配置 放在scr目錄下

<?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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Struts2Test</display-name>
    <!--  -->
<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>
     <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>

    <!-- 配置歡迎頁 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

action寫法  目錄:src.com.action

package com.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;   
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.service.PersonService;

@Namespace("/person") //訪問路徑的包名
@Results( { @Result(name = "success", location = "/index.jsp"), 
        @Result(name = "error", location = "/hello.jsp") }) 
@ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") }) 
public class PersonAction extends ActionSupport {

    @Autowired
    private transient PersonService personService;
    public PersonAction() {
        // TODO Auto-generated constructor stub
    } 
    @Action("login")  //訪問路徑的action名, 想要訪問selUser 這個方法 地址為 http://localhost:8080/工程名/yang/login
     public String selUser(){
            System.out.println("login-index");
            personService.selPerson();
         return SUCCESS;
         }
}



dao 寫法  目錄:src.com.dao.impl  接口在 src.com.dao 此處略

package com.dao.impl;

import org.springframework.stereotype.Repository;

import com.dao.PersonDao;
@Repository("personDao")  
public class PersonDaoImpl implements PersonDao {

    public PersonDaoImpl() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void selPerson() {
        // TODO Auto-generated method stub
        System.out.println("運行dao");
    }

}



service 寫法 目錄:src.com.service.impl,接口在 src.com.service 此處略


package com.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.dao.PersonDao;
import com.service.PersonService;
@Service("personService")
public class PersonServiceImpl implements PersonService {


@Autowired
private transient PersonDao personDao   ;//注入的DAO

public PersonServiceImpl() {
// TODO Auto-generated constructor stub
}


@Override
public void selPerson() {
// TODO Auto-generated method stub
System.out.println("運行service");
personDao.selPerson(); 
}
}



注意增加這個jar包  其他spring包,struts包自備

struts2-spring-plugin-2.3.16.jar

來自:http://my.oschina.net/UpBoy/blog/194437

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!