码迷,mamicode.com
首页 > 编程语言 > 详细

SSH系列:(6)整合Spring和Struts

时间:2016-08-05 18:05:04      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:ssh


首先,Spring和Struts需要整合到java web当中去,因此需要在web.xml中对struts和spring进行注册

其次,Spring和Struts整合的关键是:Struts中的action类交由Spring的IOC容器创建。


(1)添加jar包

(2)配置

(3)action代码准备

(4)注册action(分别在Spring和Struts中)

(5)添加JSP页面


1、添加jar包

引入struts jar包和spring web的jar包


struts的jar包(struts-2.3.29)


 commons-fileupload-1.3.1.jar

 commons-io-2.2.jar

 commons-lang3-3.2.jar

 commons-logging-1.1.3.jar

 freemarker-2.3.22.jar

 javassist-3.11.0.GA.jar

 ognl-3.0.17.jar

 struts2-core-2.3.29.jar

 xwork-core-2.3.29.jar



spring-web的jar包


 spring-web-3.2.5.RELEASE.jar (属于spring)

 struts2-spring-plugin-2.3.29.jar (属于struts2)




2、配置


2.1、在web.xml中注册Struts和Spring

<?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">

	<!-- 注册Spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 注册Struts -->
	<filter>
		<filter-name>struts</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>

	<display-name>Tax System</display-name>	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


2.2、添加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.enable.DynamicMethodInvocation" value="false" />
    <!-- 配置成开发模式 -->
    <constant name="struts.devMode" value="true" />
	<!-- 配置拓展名为action -->
	<constant name="struts.action.extention" value="action" />
	<!-- 把主题配置成simple -->
	<constant name="struts.ui.theme" value="simple" />
	<!-- 设置创建对象的工厂 -->
	<constant name="struts.objectFactory" value="spring" />
    

	
<!--
     <package name="default" namespace="/" extends="struts-default">
        <action name="" class="">
            <result name="success"></result>
        </action>
    </package>
 -->    
</struts>




3、action代码准备

package com.rk.test.action;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.rk.test.entity.Person;
import com.rk.test.service.PersonService;

public class PersonAction extends ActionSupport {
	private PersonService personService;
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	
	public String findById() throws Exception {
		String id = "4028d081564ba79401564ba7968e0000";
		Person p = personService.findById(id);
		ActionContext context = ActionContext.getContext();
		Map<String,Object> request = (Map<String, Object>) context.get("request");
		request.put("person", p);
		return Action.SUCCESS;
	}
	
}



4、注册action(分别在Spring和Struts中)


4.1、在spring中注册action类


将action类注册到bean-action.xml文件中。注意:action注册要使用scope="prototype"。

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx
     	http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<bean id="personAction" class="com.rk.test.action.PersonAction" scope="prototype">
		<property name="personService" ref="personService"></property>
	</bean>

</beans>

将bean-action.xml注册到applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx
     	http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	<import resource="classpath:bean-base.xml"/>
	<import resource="classpath:com/rk/*/config/bean-*.xml"/>

</beans>

其实,我们并没有修改applictionContext.xml的内容,是因为下面这句

<import resource="classpath:com/rk/*/config/bean-*.xml"/>

因为它使用了“*”号来匹配


4.2、在struts中注册action类


将action类注册到struts-test.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>
     <package name="test_package" namespace="/test" extends="struts-default">
        <action name="psn_*" class="personAction" method="{1}">
            <result name="success">/WEB-INF/jsp/test/success.jsp</result>
        </action>
    </package>
</struts>

将struts-test.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.enable.DynamicMethodInvocation" value="false" />
    <!-- 配置成开发模式 -->
    <constant name="struts.devMode" value="true" />
	<!-- 配置拓展名为action -->
	<constant name="struts.action.extention" value="action" />
	<!-- 把主题配置成simple -->
	<constant name="struts.ui.theme" value="simple" />
	<!-- 设置创建对象的工厂 -->
	<constant name="struts.objectFactory" value="spring" />
    
	<include file="com/rk/test/config/struts-test.xml"/>

	
<!--
     <package name="default" namespace="/" extends="struts-default">
        <action name="" class="">
            <result name="success"></result>
        </action>
    </package>
 -->    
</struts>

重点是添加了下面这一句

<include file="com/rk/test/config/struts-test.xml"/>



5、添加JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body>
    <table border="1">
    	<tr>
    		<td>ID:</td>
    		<td>
    			<s:property value="#request.person.pId"/>
    		</td>
    	</tr>
    	<tr>
    		<td>姓名:</td>
    		<td>
    			<s:property value="#request.person.pName"/>
    		</td>
    	</tr>
    	<tr>
    		<td>版本:</td>
    		<td>
    			<s:property value="#request.person.pVersion"/>
    		</td>
    	</tr>
    	
    </table>
  </body>
</html>

访问地址

http://127.0.0.1:8080/tax/test/psn_findById.action

显示结果

技术分享




SSH系列:(6)整合Spring和Struts

标签:ssh

原文地址:http://lsieun.blog.51cto.com/9210464/1834728

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!