Struts2+Spring3 基于注解的配置

web.xml 配置如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<? 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目录下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<? 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目录下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<? 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 此处略

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 此处略 


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值