`
KuangYeYaZi
  • 浏览: 57845 次
文章分类
社区版块
存档分类
最新评论

Struts2+Spring3 基于注解的配置

 
阅读更多

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

 

http://www.open-open.com/lib/view/open1390793407836.html

 

分享到:
评论

相关推荐

    基于Struts2+hibernate+spring 注解开发的学生信息管理系统

    总结来说,这个"基于Struts2+hibernate+spring 注解开发的学生信息管理系统"是一个利用Java Web三大框架的典型应用,实现了学生信息的基本操作。通过优化前端设计,可以进一步提升系统的整体质量和用户体验。在实际...

    struts2+spring+mybatis+easyui的实现

    接着,配置Struts2、Spring和MyBatis的初始化参数,确保它们能正确加载和初始化。最后,编写Action、Service、DAO以及EasyUI对应的JSP页面,完成业务逻辑和界面展示。 总的来说,"struts2+spring+mybatis+easyui"的...

    struts2 + spring + mybatis 注解开发

    在Struts2中,我们可以使用注解来简化配置。例如,`@Action`注解用于标记一个类为Struts2的Action,`@Results`注解用于定义Action执行后的结果页面。例如: ```java import com.opensymphony.xwork2.ActionSupport;...

    struts2+spring+Ibatis框架包

    在"ssi_jar"这个压缩包中,可能包含了这三个框架的库文件,例如struts2相关的jar包(如struts2-core、struts2-convention等)、Spring的核心库(如spring-context、spring-web等)以及iBatis的库文件(如ibatis-3-...

    Struts2+spring注解配置简介

    本篇文章将详细介绍如何在Struts2和Spring框架中结合使用注解进行配置,以简化开发过程。 首先,让我们了解Spring框架中的注解配置。Spring提供了如@Component、@Service、@Repository和@Controller等注解,用于...

    struts2+spring+mybatis框架

    在Struts2+Spring+MyBatis的架构中,MyBatis负责与数据库交互,通过XML或注解方式配置SQL语句,使得数据库操作更加灵活且易于维护。 **整合过程** 1. **配置Spring**:创建Spring配置文件,定义Bean,包括Action、...

    struts2+spring+ibatis+mysql

    "Struts2+Spring+Ibatis+MySQL" 是一个经典的Java Web开发框架组合,用于构建高效、可扩展的企业级应用程序。这个组合集成了强大的MVC(Model-View-Controller)框架Struts2、依赖注入与面向切面编程的Spring框架、...

    struts2+spring练习

    1. **Struts2框架**:Struts2是基于Action的MVC框架,它的核心是`struts.xml`配置文件,用于定义Action类及其对应的结果页面。通过`&lt;action&gt;`标签,你可以设置Action的名称、类和结果。Struts2使用拦截器来处理请求...

    struts1.2 + spring2.5 + hibernate3.2框架demo

    2. **配置文件**:struts-config.xml定义Struts的配置,spring-beans.xml管理Spring的bean,hibernate.cfg.xml配置Hibernate的数据库连接,可能还有实体类的映射文件(hbm.xml或使用注解)。 3. **JSP页面**:展示...

    Struts+Spring+Hibernate注解零配置整合

    本项目"Struts+Spring+Hibernate注解零配置整合"的目标是展示如何通过注解方式实现这三大框架的无缝集成,从而减少XML配置文件的使用,提高开发效率和代码可读性。 在传统的Struts、Spring和Hibernate整合中,大量...

    Struts2+Spring3+Hibernate4零配置所需全部jar包

    例如,Struts2的struts-default.xml、Spring的applicationContext.xml和Hibernate的hibernate.cfg.xml等,都已经被预设好或者通过注解方式进行配置。 在实际开发中,这三个框架的集成可以带来诸多优势:Struts2负责...

    Struts2+spring注解配置

    Struts2 和 Spring 的整合是企业级 Java Web 开发中常见的技术栈,通过注解配置可以使项目更加简洁、易于维护。Struts2 提供了一种使用注解替代 XML 配置的方式,使得开发者无需编写繁琐的 struts.xml 文件,即可...

    struts2+spring+hibernate配置

    总结来说,"struts2+spring+hibernate配置"涉及到了Java Web开发中的三层架构,包括前端控制、业务逻辑和数据持久化。开发者需要理解这三个框架的核心原理,并掌握它们的配置和整合方法,以构建高效、可维护的Web...

    struts2+mybatis+spring 注解很好的例子

    1. 配置Spring:创建Spring配置文件,启用注解扫描,并配置数据源、事务管理器以及Struts2和MyBatis的插件。 2. 创建Action:定义一个Action类,使用`@Action`注解指定处理的请求,同时通过`@Autowired`注解注入...

    Struts2+Spring3+MyBatis结合项目

    Struts2、Spring3和MyBatis是Java Web开发中常用的三大框架,它们各自负责不同的职责,协同工作能够构建出高效、松耦合的Web应用。这个“Struts2+Spring3+MyBatis结合项目”是一个实战示例,旨在帮助开发者深入理解...

    Struts2+Spring+Mybaits3框架整合实例

    Struts2、Spring和MyBatis3是Java Web开发中常用的三大开源框架,它们的整合应用可以构建出高效、灵活的MVC(Model-View-Controller)架构。本实例将详细解析这三大框架如何协同工作,提升开发效率,并提供一个基于...

    基于注解的Spring+Struts2+Mybatis+Maven项目

    【基于注解的Spring+Struts2+Mybatis+Maven项目详解】 在现代Web开发中,Spring、Struts2、Mybatis和Maven是四个非常重要的组件,它们各自负责不同的职责,共同构建了一个高效、灵活且易于维护的Java Web应用。这个...

    Struts2+Spring4+Hibernate5整合

    3. **配置Spring**:创建Spring的配置文件(如applicationContext.xml),声明Bean,包括Action类、DAO接口及其实现、Service层接口及其实现。使用@Autowired注解实现依赖注入。 4. **配置Hibernate**:设置...

Global site tag (gtag.js) - Google Analytics