spring_SSH整合之_Struts2_Spring_Plugin的详解

来源:互联网 发布:怎样破解公司网络限制 编辑:程序博客网 时间:2024/05/16 05:44
Action.java里面属性是由struts2-spring-Plugin.jar插件来去控制注入到spring但是必须得setXxx()<!-- 与spring集合时,指定由spring负责action对象的创建(自动装配) --><constant name="struts.objectFactory.spring.autoWire" value="type" />


结伦:Action不用去手动去spring注入了,由Plugin插件自动按type注入。Spring容器里面不用写Action有关的注入


package com.bjsxt.registration.action;import java.util.List;import com.bjsxt.registration.model.User;import com.bjsxt.registration.service.UserManager;import com.bjsxt.registration.vo.UserRegisterInfo;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class UserAction extends ActionSupport implementsModelDriven<UserRegisterInfo> {private UserRegisterInfo info;private UserManager userManager;private List<User> users;private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public UserRegisterInfo getInfo() {return info;}public void setInfo(UserRegisterInfo info) {this.info = info;}public UserManager getUserManager() {return userManager;}public void setUserManager(UserManager userManager) {this.userManager = userManager;}public List<User> getUsers() {return users;}public void setUsers(List<User> users) {this.users = users;}@Overridepublic String execute() throws Exception {User user = new User();user.setUsername(info.getUsername());user.setPassword(info.getPassword());if (userManager.exists(user)) {return "FAIL";}userManager.add(user);return "SUCCESS";}public UserRegisterInfo getModel() {return info;}public String list() {this.users = this.userManager.getUsers();return "LIST";}public String load() {this.user = this.userManager.loadById(info.getId());this.info.setId(this.user.getId());this.info.setUsername(this.user.getUsername());this.info.setPassword(this.user.getPassword());return "LOAD";}}

<?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.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 依赖注入UserDaoImpl --><bean id="userDaoImpl" class="com.bjsxt.registration.dao.impl.UserDaoImpl"><property name="hibernateTemplate" ref="hibernateTemplate" /><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 依赖注入UserManagerImpl --><bean id="userManagerImpl" class="com.bjsxt.registration.service.impl.UserManagerImpl"><property name="userDao" ref="userDaoImpl" /></bean><!-- 配置DataSource --><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><value>classpath:jdbc.properties</value></property></bean><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><!-- 配置SessionFactory --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mappingDirectoryLocations"><list><value>classpath:com/bjsxt/registration/model/</value></list></property><property name="hibernateProperties"><props><!-- 显示SQL语句 --><prop key="hibernate.show_sql">true</prop><!-- 自动构建表结构 --><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!-- 配置HibernateTeimplate --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置事务txManager --><bean id="txMeanage"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置AOP范围 --><aop:config><aop:pointcutexpression="execution(public * com.bjsxt.registration.service..*.*(..))"id="pointcut" /><aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /></aop:config><!-- 配置事务范围txAdvisor --><tx:advice id="txAdvice" transaction-manager="txMeanage"><tx:attributes><tx:method name="exists" read-only="true" /><tx:method name="get*" read-only="true" /><tx:method name="add*" propagation="REQUIRED" /></tx:attributes></tx:advice></beans>

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%@ taglib uri="/struts-tags" prefix="s"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>load列表</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"></head><body><a href="bjsxt/user_list.action">用户列表</a><br /><s:property value="username" /><s:debug></s:debug></body></html>

package com.bjsxt.registration.action;import java.util.List;import org.springframework.context.ApplicationContext;import com.bjsxt.registration.model.User;import com.bjsxt.registration.service.UserManager;import com.bjsxt.registration.vo.UserRegisterInfo;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class UserAction extends ActionSupport implementsModelDriven {private UserRegisterInfo info;private UserManager userManager;private List users;public UserRegisterInfo getInfo() {return info;}public void setInfo(UserRegisterInfo info) {this.info = info;}public UserManager getUserManager() {return userManager;}public void setUserManager(UserManager userManager) {this.userManager = userManager;}public List getUsers() {return users;}public void setUsers(List users) {this.users = users;}@Overridepublic String execute() throws Exception {User user = new User();user.setUsername(info.getUsername());user.setPassword(info.getPassword());if (userManager.exists(user)) {return "FAIL";}userManager.add(user);return "SUCCESS";}public UserRegisterInfo getModel() {return info;}public String list() {this.users = this.userManager.getUsers();return "LIST";}}/registSuccess.jsp/registFail.jsp/userlist.jsp<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%@ taglib uri="/struts-tags" prefix="s"%>用户列表用户名密码

阅读全文
0 0
原创粉丝点击