Struts2+Spring3 基于注解的配置

原创
2014/01/20 15:41
阅读数 4.5K
AI总结

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

展开阅读全文
加载中
点击加入讨论🔥(4) 发布并加入讨论🔥
4 评论
10 收藏
0
分享
AI总结
返回顶部
顶部