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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:Beans.xml
</param-value>
</context-param> <!--啟動Spring的Listener -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <!--編碼Filter,不過貌似沒用 -->
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- Hibernate 延時加載 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class> <init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!--Struts2 -->
<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>
</web-app>

Beans.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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">
<!--注解支持 -->
<context:annotation-config />
<!--让Spring扫描的包 -->
<context:component-scan base-package="zp.ssh..*"> </context:component-scan>
<!-- 数据库文件位置 -->
<context:property-placeholder location="classpath*:jdbc.properties" />
<!--配置dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <!-- hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="zp.ssh"></property> <property name="mappingLocations">
<list>
</list>
</property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="current_session_context_class">thread</prop> </props>
</property>
</bean> <!-- <bean id="hl" class="zp.ssh.action.HelloWorldAction"/> --> </beans>

Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<!-- 用Spring产生Action -->
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="user" extends="struts-default">
<!-- hl是Action类中的注解名称。因为要交给Spring管理,所以class属性不写完整的类名,写Spring装载的Bean的标识符 -->
<action name="helloworld" class="hl" >
<result name="success">welcome.jsp</result>
<result name="error">error.jsp</result>
<result name="input">index.jsp</result>
</action> </package> </struts>

示例action

package zp.ssh.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport; import zp.ssh.service.MyService;
//这个hl就是struts.xml中的class属性填的东西
@Component("hl")
public class HelloWorldAction extends ActionSupport { @Autowired
private MyService myService;
public HelloWorldAction(){
System.out.println("action被创建");
}
public String execute() throws Exception {
myService.Service();
return SUCCESS;
}
}

ssh整合随笔(注解方式,Spring 管理action)的更多相关文章

  1. Spring Boot整合Mybatis(注解方式和XML方式)

    其实对我个人而言还是不够熟悉JPA.hibernate,所以觉得这两种框架使用起来好麻烦啊. 一直用的Mybatis作为持久层框架, JPA(Hibernate)主张所有的SQL都用Java代码生成, ...

  2. ssh整合之七注解结合xml形式

    1.我们之前的纯xml的方式,我们的配置文件很多,我们可以使用注解结合xml的方式进行开发,这样的话,我们的配置文件,会少很多,同时,我们可以直接在类中看到配置,这样,我们就可以快速地搭建一个ssh整 ...

  3. Java进阶知识26 SSH整合(Struts2、Spring、Hibernate)

    1.我用到的jar包 2.整合实例 2.1.数据库建表语句 create database school; -- 创建数据库 use school; -- 使用school数据库 create tab ...

  4. SSH整合(Struts2+hibernate+spring)

    1.创建表 create table t_user( id int primary key auto_increment, username varchar(50), password varchar ...

  5. 在ssh框架中注解方式需要注意的几个问题

    1.注解方式的时候 Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get ...

  6. Ibatis,Spring整合(注解方式注入)

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  7. ssh整合之五struts和spring整合

    1.首先,我们需要先分析一下,我们的spring容器在web环境中,只需要一份就可以了 另外,就是我们的spring容器,要在我们tomcat启动的时候就创建好了(包括其中的spring的对象),怎么 ...

  8. SSH整合 第三篇 Spring的加入

    1.思路和想法. 目前理解到的,觉得是的,可能的,应该这样的……………… Spring的两大核心是IoC和AOP Ioc:帮助实例化对象,加载到容器中,在注入到需要用到的地方.这样就可以减少在不同的方 ...

  9. 零基础学习java------40---------Maven(maven的概念,安装,maven在eclipse中使用),springboot(spring整合springmvc(注解),spring整合mybatis(常见的配置文件)),前端页面(bootstrap软件)

    一 maven 1. Maven的相关概念 1.1 项目开发中遇到的问题 (1)都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行? (2)为什么在我的机器上可以正常打包,而配置管理 ...

随机推荐

  1. ES6—解构赋值

    1.什么是解构赋值 ES6允许按照预定的模式,从数组.对象中提取值,对变量进行赋值. 我们直接用例子说明.    2. 数组的解构赋值 数组传统的变量赋值:      var arr=[1,2,3]; ...

  2. 最大子段和问题,最大子矩阵和问题,最大m子段和问题

    1.最大子段和问题      问题定义:对于给定序列a1,a2,a3……an,寻找它的某个连续子段,使得其和最大.如( -2,11,-4,13,-5,-2 )最大子段是{ 11,-4,13 }其和为2 ...

  3. printf 格式化输出符号详细说明(转)

    %a             浮点数.十六进制数字和p-记数法(C99)%A 浮点数.十六进制数字和p-记法(C99)%c 一个字符(char) %C           一个ISO宽字符 %d 有符 ...

  4. Android项目——传感器的使用

    public class MainActivity extends Activity { // 定义 方向传感器 和 重力传感器 private TextView tvOrientation, tvA ...

  5. memcache中的add和set方法区别

    相信大家对memcache都不陌生,在项目中也经常使用memcache作为缓存方案,那么在使用过程中有没有发现为什么memcahce有两个添加缓存的方法:一个是add,一个是set,那么你知道这2个方 ...

  6. Navicat导入数据时发生了报错 --- 1153 - Got a packet bigger than 'max_allowed的处理办法

    今天我在使用Navicat导入.sql文件数据时,发现本来是80万条的数据,结果只导入了10万条左右,而且在其错误信息日志中,我发现了这样一条错误:1153 - Got a packet bigger ...

  7. Linux 数据流重定向

    1.三种数据流重定向1)标准输入(stdin):代码为0,使用0<或0<<,其中代码0可以省略2)标准输出(stdout):代码为1,使用1>或1>>,其中代码1可 ...

  8. JSON 序列化和反序列化——JavaScriptSerializer实现

    一. JavaScriptSerializer 类由异步通信层内部使用,用于序列化和反序列化在浏览器和 Web 服务器之间传递的数据.您无法访问序列化程序的此实例.但是,此类公开了公共 API.因此, ...

  9. vs2010设置 "行号显示"

    Microsoft Visual Studio 2010 默认情况下是不显示代码的行号的.在编译出错时,可点击下面输出窗口中的错误提示进行定位. 但是这样操作起来你有没有感觉到不方便呢. 不显示行号时 ...

  10. CentOS 6使用mutt+msmtp发送邮件

    转:http://www.tuicool.com/articles/YRnQVfq CentOS系统下如果希望向外域发送邮件,需要配置sendmail+dovecot等一系列工具.其实不用这么麻烦,只 ...