spring整合struts2

刚学Struts2,在整合Spring的时候遇到了不少麻烦,想在网上找简单的例子却老是找不到,经过一番折腾,终于完成了如下简单得不得了的小例子。
开发环境是:Windows Server 2008 + jdk6.0 + myeclipse6.5 blue + struts-2.0.11 + Spring2.0 + Tomcat6.0
新建一个web Porject,名称为Struts2Spring, J2EE Specification level(J2EE版本) 选择 JAVA EE 5.0, 然后为该项目加入Sturts2的支持,
右键项目 -> Bilud Path -> ConfigBiludPath -> Libraries -> Add External JARs然后添加struts2的类包.
这里注意的是必须加入了Struts2-spring-plugin-2.0.11.jar。
紧接着,再添加对spring的支持,
右键项目 -> MyEclipse-> AddSpringCapabilities .
这里需要特别注意的是,在选择Spring类包的时候,必须勾上Spring 2.0 Web Libraries,否则在启动Tomcat服
务器的时候就回报错,好像是Spring的监听器什么什么的,然后直接点击 Finish 完成对Spring支持的添加
接着编写LoginService.java这个接口,具体代码如下:
1
package org.lmxzz.struts2.service;
2

3
public interface LoginService
4
{
5
public boolean doLogin(String userName, String password) ;
6
}

再编写实现类LoginServiceImpl,具体代码如下:
01
package org.lmxzz.struts2.service.impl;
02

03
import org.lmxzz.struts2.service.LoginService;
04

05
public class LoginServiceImpl implements LoginService
06
{
07
public boolean doLogin(String userName, String password)
08
{
09
boolean flag = false ;
10

11
if("LmxZz".equals(userName) && "3348635".equals(password))
12
flag = true ;
13

14
return flag ;
15
}
16
}

接着是LoginAction.java的具体代码:
01
package org.lmxzz.struts2.action;
02

03
import org.lmxzz.struts2.service.LoginService;
04

05
import com.opensymphony.xwork2.ActionSupport;
06

07
public class LoginAction extends ActionSupport
08
{
09
private String userName ;
10
private String password ;
11
private LoginService loginService ;
12

13
public String getUserName()
14
{
15
return userName;
16
}
17

18
public void setUserName(String userName)
19
{
20
this.userName = userName;
21
}
22

23
public String getPassword()
24
{
25
return password;
26
}
27

28
public void setPassword(String password)
29
{
30
this.password = password;
31
}
32

33
public void setLoginService(LoginService loginService)
34
{
35
this.loginService = loginService;
36
}
37

38
@Override
39
public String execute() throws Exception
40
{
41
if(loginService.doLogin(userName, password))
42
return SUCCESS ;
43
else
44
return INPUT ;
45
}
46
}

接着,修改index.jsp文件,修改后代码如下:
01
<%@ page language="java" pageEncoding="UTF-8"%>
02
<%@ taglib prefix="s" uri="/struts-tags" %>
03
<html>
04
<head>
05
<title>ndex.jsp</title>
06
</head>
07

08
<body>
09
<s:form action="login">
10
<s:textfield name="userName" label="userName"/>
11
<s:textfield name="password" label="password"/>
12
<s:submit/>
13
</s:form>
14
</body>
15
</html>

这里需要注意的是<s:form action="login"> 中的 login,具体要注意什么在struts.xml里再进行说明
下面是重要的struts.xml 和 applicationContext.xml 配置文件,具体代码分别如下:
01
<?xml version="1.0" encoding="UTF-8"?>
02
<!DOCTYPE struts PUBLIC
03
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
04
"http://struts.apache.org/dtds/struts-2.0.dtd">
05
<struts>
06
<constant name="struts.objectFactory" value="spring"/>
07
<package name="struts2" extends="struts-default">
08
<action name="login" class="loginAction">
09
<result>success.jsp</result>
10
<result name="input">index.jsp</result>
11
</action>
12
</package>
13
</struts>


下面的是applicationContext.xml :
01
<?xml version="1.0" encoding="UTF-8"?>
02
<beans
03
xmlns="http://www.springframework.org/schema/beans"
04
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
06

07
<bean id="loginService" class="org.lmxzz.struts2.service.impl.LoginServiceImpl"/>
08

09
<bean id="loginAction" class="org.lmxzz.struts2.action.LoginAction" scope="prototype">
10
<property name="loginService">
11
<ref bean="loginService"/>
12
</property>
13
</bean>
14
</beans>

首先,<action name="login" class="loginAction"> 中的 name="login",
这个login必须与index.jsp中的action="login"保持一致
class="loginAction"这里的loginAction不再是以前的真正的类的映射,如class="org.lmxzz.struts2.action.LoginAction"
因为要交给spring管理,所以这里的loginAction 必须 要与 applicationContext.xml 中的
<bean id="loginAction" class="org.lmxzz.struts2.action.LoginAction" scope="prototype">
的 id="loginAction" 保持一致,这里的class="org.lmxzz.struts2.action.LoginAction"就是真正的类的映射,
这样写,就表明了struts.xml中的loginAction 已交给 spring来进行管理。
scope="prototype" 这里和以前的struts1.x整合spring的时候不一样,因为strust1.x对action的管理是单例模式。
完成了上面的工作以后,只是相当于完成了整个项目的一半,而最为重要的 web.xml 的代码如下:
01
<?xml version="1.0" encoding="UTF-8"?>
02
<web-app version="2.5"
03
xmlns="http://java.sun.com/xml/ns/javaee"
04
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
06
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
07

08
<listener>
09
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10
</listener>
11
<filter>
12
<filter-name>struts2</filter-name>
13
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
14
</filter>
15
<filter-mapping>
16
<filter-name>struts2</filter-name>
17
<url-pattern>/*</url-pattern>
18
</filter-mapping>
19
<welcome-file-list>
20
<welcome-file>index.jsp</welcome-file>
21
</welcome-file-list>
22
</web-app>

这里最需要注意的是:
1
<listener>
2
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3
</listener>
它为服务器添加了一个监听器,这样也使的struts2 与 spring 结合起来。如果没有这句话,服务器是启动不了的。
剩下的就是一个success.jsp页面,这里就不再详说这个页面了。
完成了以后,启动服务器,如没有意外的话,系统会报错,说找不到 applicationContext.xml 配置文件,这个
时候我们停止服务器,把 applicationContext.xml 移动到 WebRoot中的WEB-INF目录下,重新启动服务器,
进入页面后输入用户名:LmxZz,密码:3348635 系统就会转到 success.jsp页面去了.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值