Spring3.x整合Struts2.3.x

Spring3.x整合Struts2.3.x

在配置完Struts2.3.x环境:http://blog.csdn.net/tangzenglei/article/details/46984591,下面对spring和Struts2.3.x进行整合。

1.环境搭建

下载方法:http://blog.csdn.net/a385833253/article/details/17263475

序号

名称

描述

1

struts2-spring-plugin-2.3.12.jar

Struts2+Spring整合包

2

spring-core-3.2.2.jar

Spring核心工具包

3

spring-context-3.2.2.jar

为Spqing提供核心扩展

4

spring-context-support-3.2.2.jar

为Spqing提供核心扩展

5

spring-beans-3.2.2.jar

管理beans的工具

6

spring-expression-3.2.2.jar

表达式语言

7

spring-web-3.2.2.jar

Web应用开发包

8

spring-aspects-3.2.2.jar

提供对AspectJ的支持,以便可以方便的将面向方面的功能集成进IDE中



2.配置文件

2.1  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
  <display-name></display-name>	
	
	<!-- openSessionInView配置 -->
	<!-- <filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>*.action</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>
	
	
		<!-- spring配置文件位置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml</param-value>
	</context-param> 
	
	<!-- spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	
	<!-- 欢迎登录界面 -->
	<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>

	<!-- 指定由spring负责action对象的创建 -->
	<!-- <constant name="struts.objectFactory" value="spring" /> -->
	<!-- 所有匹配的请求都由struts2处理 -->
	<constant name="struts.action.extension" value="action" />
	<!-- 是否启用开发模式 -->
	<constant name="struts.devMode" value="false" />
	<!-- struts配置文件改动后,是否重新加载 -->
	<constant name="struts.configuration.xml.reload" value="true" />
	<!-- 设置浏览器是否缓存静态内容 -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 请求参数的编码方式 -->
	<constant name="struts.i18n.encoding" value="utf-8" />
	<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
	<constant name="struts.i18n.reload" value="true" />
	<!-- 让struts2支持动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<!-- Action名称中是否还是用斜线 -->
	<constant name="struts.enable.SlashesInActionNames" value="false" />
	<!-- 允许标签中使用表达式语法 -->
	<constant name="struts.tag.altSyntax" value="true" />
	<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
	<constant name="struts.dispatcher.parametersWorkaround" value="false" />
	<!-- 把主题改为simple -->
	<constant name="struts.ui.theme" value="simple"></constant>
	
	<package name="basePackage" extends="struts-default">
	     
		<action name="login_*" class="loginAction" method="{1}">
			<result name="success">/WEB-INF/jsp/loginAction/login.jsp</result>
			<result name="error">/WEB-INF/jsp/loginAction/error.jsp</result>
		</action> 
		
	</package>
	
</struts>

注意:class是loginAction,在spring中进行了配置

2.3 spring.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" 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
">
	 <!-- singleton="false" -->
	<bean id="loginAction" class="cn.order.action.LoginAction">
	</bean>
</beans>

注意:此处spring的作用体现了出来,即将所有类进行配置和装载从而在struts.xml中可以正确识别并使用。

3.mvc实现

3.1 LoginAction.java


package cn.order.action;
import cn.order.model.User;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction implements ModelDriven<User>{

	User model=new User();
	@Override
	public User getModel() {
		return model;
	}
	
	public String list(){
		if("123".equals(model.getI_password())&&"tang".equals(model.getI_userName())){
			return "success";
		}
		else{
			return "error";
		}
	}

}



3.2 login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录</title>
</head>
<body>	
	login success!
</body>
</html>

3.3 error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录</title>
</head>
<body>	
	Error.jsp
</body>
</html>


4.结果与总结

Spring3.x整合Struts2.3.x_第1张图片
当用户名和密码失败时:
差
当用户名和密码匹配时:
好

你可能感兴趣的:(Spring3.x整合Struts2.3.x)