struts2.3+hibernate4.2+spring3.2整合 (二)

9 篇文章 0 订阅

一、先对sping和hibernate进行整合。以前写过,在这就不详细写了。详情请看http://blog.csdn.net/zhangzhuo6663196/article/details/26078165

1、jar包的引入

2、sessionFactory 的注入

3、各组件,各注入。

二、sping和struts的整合。        Struts2 的action 由Spring产生

1、jar包的引入,以及struts-spring-plugin、spring-web 包的引入。   

2、修改web.xml文件,加入struts 的filter

3、再加入spring的listener ,根据需要修改配置文件地址。        webapp一旦启动,spring容器就会初始化。

web.xml   文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list> 

  <!-- sping  找到配置文件,启动容器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    <!-- 默认找文件    /WEB-INF/applicationContext.xml  -->
  </listener>  

  <!-- 配置spring 配置文件地址 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
<!--     <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
  </context-param>
  
  <!-- struts -->
  <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>     

4、规划struts 的action 和jsp展现

5、加入struts.xml .  

修改配置,由spring 替代struts产生action对象 。      直接引入struts-spring-plugin包即可,配置在struts-plugin.xml中

struts.xml文件配置不用改:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<!--     <constant name="struts.devMode" value="true" /> -->
<!--     <constant name="struts.enable.DynamicMethodInvocation" value="true" />  动态调用 -->
<!--      <constant name="struts.ognl.allowStaticMethodAccess" value="true"/>  -->
    <!-- <constant name="struts.i18n.encoding" value="GBK"/>--><!-- 理论上来解决中文乱码问题,但有BUG没解决 -->
    
    <package name="dafault" namespace="/" extends="struts-default">
        <default-action-ref name="register"></default-action-ref>
        
        <action name="register" class="com.ssh.action.RegisterAction">
            <result name="success">/success.jsp</result>
            <result name ="error">/false.jsp</result>
        </action>
    </package>
  
</struts>

6、修改action配置

把类名改为bean对象的名称,可以用首字母小写。

@Scope("prototype") 不要忘记    每个请求产生一个action

package com.ssh.action;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.model.User;
import com.ssh.service.UserService;

@Component("register")
@Scope("prototype")       //设置多例
public class RegisterAction extends ActionSupport{
	
	private String username;
	private String password;
	private String password2;
	
	private UserService userService;
	
	@Override
	public String execute(){
		System.out.println("1111");
		User user = new User();
		user.setUserName(username);
		user.setPassword(password);
		
		if(userService.exist(username)){
			return ERROR;
		}
		userService.save(user);
		return SUCCESS;		
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public UserService getUserService() {
		return userService;
	}
	
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}


}


三、struts读常量的顺序

1、struts-dafalut.xml

2、struts-plugin.xml

3、struts.xml

4、struts.properties

5、web.xml


注:jar包导入后,要进行检查是否有冲突。最好全导在lib下面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值