刚学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 | <font face= "Verdana" > package org.lmxzz.struts2.service; |
3 | public interface LoginService |
5 | public boolean doLogin(String userName, String password) ; |
再编写实现类LoginServiceImpl,具体代码如下:
01 | <font face= "Verdana" > package org.lmxzz.struts2.service.impl; |
03 | import org.lmxzz.struts2.service.LoginService; |
05 | public class LoginServiceImpl implements LoginService |
07 | public boolean doLogin(String userName, String password) |
09 | boolean flag = false ; |
11 | if ( "LmxZz" .equals(userName) && "3348635" .equals(password)) |
接着是LoginAction.java 的具体代码:
01 | <font face= "Verdana" > package org.lmxzz.struts2.action; |
03 | import org.lmxzz.struts2.service.LoginService; |
05 | import com.opensymphony.xwork2.ActionSupport; |
07 | public class LoginAction extends ActionSupport |
09 | private String userName ; |
10 | private String password ; |
11 | private LoginService loginService ; |
13 | public String getUserName() |
18 | public void setUserName(String userName) |
20 | this .userName = userName; |
23 | public String getPassword() |
28 | public void setPassword(String password) |
30 | this .password = password; |
33 | public void setLoginService(LoginService loginService) |
35 | this .loginService = loginService; |
39 | public String execute() throws Exception |
41 | if (loginService.doLogin(userName, password)) |
接着,修改index.jsp文件,修改后代码如下:
01 | < font face = "Verdana" ><%@ page language="java" pageEncoding="UTF-8"%> |
02 | <%@ taglib prefix="s" uri="/struts-tags" %> |
05 | < title >ndex.jsp</ title > |
09 | < s:form action = "login" > |
10 | < s:textfield name = "userName" label = "userName" /> |
11 | < s:textfield name = "password" label = "password" /> |
这里需要注意的是<s:form action="login"> 中的 login,具体要注意什么在struts.xml里再进行说明
下面是重要的struts.xml 和 applicationContext.xml 配置文件,具体代码分别如下:
01 | < font face = "Verdana" ><? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
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 > |
下面的是applicationContext.xml :
01 | < font face = "Verdana" ><? xml version = "1.0" encoding = "UTF-8" ?> |
07 | < bean id = "loginService" class = "org.lmxzz.struts2.service.impl.LoginServiceImpl" /> |
09 | < bean id = "loginAction " class = "org.lmxzz.struts2.action.LoginAction" scope = "prototype" > |
10 | < property name = "loginService" > |
11 | < ref bean = "loginService" /> |
首先,<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 | < font face = "Verdana" ><? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < web-app version = "2.5" |
09 | < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > |
12 | < filter-name >struts2</ filter-name > |
13 | < filter-class >org.apache.struts2.dispatcher.FilterDispatcher</ filter-class > |
16 | < filter-name >struts2</ filter-name > |
17 | < url-pattern >/*</ url-pattern > |
20 | < welcome-file >index.jsp</ welcome-file > |
21 | </ welcome-file-list > |
这里最需要注意的是:
<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> | |
它为服务器添加了一个监听器,这样也使的struts2 与 spring 结合起来。如果没有这句话,服务器是启动不了的。
剩下的就是一个success.jsp页面,这里就不再详说这个页面了。
完成了以后,启动服务器,如没有意外的话,系统会报错,说找不到 applicationContext.xml 配置文件,这个
时候我们停止服务器,把 applicationContext.xml 移动到 WebRoot中的WEB-INF目录下,重新启动服务器,
进入页面后输入用户名:LmxZz,密码:3348635 系统就会转到 success.jsp页面去了.
原址:http://dev.firnow.com/course/3_program/java/javajs/200866/122777.html