1.Spring整合struts

本文详细介绍了如何将 Struts、Spring 和 Web 应用进行整合,包括配置文件、容器管理及具体实现过程。

流程为struts处理web上的事件,而struts的action组件交给spring管理,struts的配置文件只要引用spring里的action组件的id即可

web.xml需要处理(源码来自李刚的javaee开发)

<?xml version="1.0" encoding="GBK"?>
<web-app 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" version="3.0">
	<!-- 1使用ContextLoaderListener初始化Spring容器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- 2定义Struts 2的FilterDispathcer的Filter -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<!-- 3FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

struts配置文件

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts 2配置文件的根元素 -->
<struts>
	<!-- 配置了系列常量 -->
	<constant name="struts.i18n.encoding" value="GBK"/>	
	<constant name="struts.devMode" value="true"/>	
	<package name="lee" extends="struts-default">
		<!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类
			, 而是Spring容器中的Bean实例-->
		<action name="loginPro" class="loginAction">
			<!-- 为两个逻辑视图配置视图页面 -->
			<result name="error">/content/error.jsp</result>
			<result name="success">/content/welcome.jsp</result>
		</action>
		<!-- 让用户直接访问该应用时列出所有视图页面 -->
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>
		</action>
	</package>
</struts>
spring的容器管理文件/SHproject/WebRoot/WEB-INF/applicationContext.xml

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<!-- 定义一个业务逻辑组件,实现类为MyServiceImp -->
	<bean id="myService" 
		class="org.crazyit.app.service.impl.MyServiceImpl"/>
	<!-- 让Spring管理的Action实例 -->
	<bean id="loginAction" class="org.crazyit.app.action.LoginAction"
		scope="prototype">
		<!-- 依赖注入业务逻辑组件 -->
		<property name="ms" ref="myService"/>
	</bean>
</beans>

org.crazyit.app.service.impl.MyServiceImpl

public class MyServiceImpl implements MyService {
	public boolean valid(String username, String pass) {
		// 此处只是简单示范,故直接判断用户名、密码
		// 是否符合要求
		if (username.equals("crazyit.org") && pass.equals("leegang")) {
			return true;
		}
		return false;
	}
}

org.crazyit.app.action.LoginAction


public class LoginAction implements Action {
	// 下面是用于封装用户请求参数的两个属性
	private String username;
	private String password;
	// 用于封装处理结果的属性
	private String tip;
	// 系统所用的业务逻辑组件
	private MyService ms;

	// 设置注入业务逻辑组件所必需的setter方法
	public void setMs(MyService ms) {
		this.ms = ms;
	}

	// username属性的setter和getter方法
	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return this.username;
	}

	// password属性所必需的setter和getter方法
	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return this.password;
	}

	// tip属性的getter和setter方法
	public void setTip(String tip) {
		this.tip = tip;
	}

	public String getTip() {
		return this.tip;
	}

	// 处理用户请求的execute方法
	public String execute() throws Exception {
		// 调用业务逻辑组件的valid方法来
		// 验证用户输入的用户名和密码是否正确
		if (ms.valid(getUsername(), getPassword())) {
			setTip("哈哈,整合成功!");
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
}

登录界面

<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>登录页面</title>
</head>
<body>
<h3>用户登录</h3>
<s:form action="loginPro">
	<s:textfield name="username" label="用户名"/>
	<s:textfield name="password" label="密码"/>
	<tr align="center">
		<td colspan="2">
		<s:submit value="登录" theme="simple"/>
		<s:reset value="重设" theme="simple"/>
		</td>
	</tr>
</s:form>
</body>
</html>

<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>成功页面</title>
</head>
<body>
	您已经登录!
	<s:property value="tip"/>
</body>
</html>
<%--
网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
author  yeeku.H.lee kongyeeku@163.com
version  1.0
Copyright (C), 2001-2012, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date: 
--%>

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>错误页面</title>
</head>
<body>
	您不能登录!
</body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值