Spring与Struts2整合

来源:互联网 发布:单反相机拍摄技巧 知乎 编辑:程序博客网 时间:2024/04/28 16:03

今天学习了在Struts2中使用Spring,记录如下:

1、首先创建web项目

2、导入Struts2需要的库,编辑web.xml和struts.xml

3、给项目添加Spring支持,注意这里的添加方式跟在普通的java工程中添加Spring是不一样的,在工程上右键-->MyEclipse-->add Spring Capabilities...出现如下图所示的对话框:


注意上面的Library中,要选择三项:

①Spring 3.0 AOP Library

②Spring 3.0 core Library

③Spring 3.0 Web Library

在web项目中,需要添加上面这三项才行,点击next之后,出现如下所示的对话框:


这里需要注意,将applicationContext.xml文件的路径选择为WEB-INF下,而不是默认的路径

4、给项目添加struts2-spring-plugin-2.1.6.jar插件,该插件是一个jar文件,在struts2的lib目录中,该插件是连接struts2和spring的关键,我在使用的过程中遇到一个奇怪的问题,如果用2.3.20的包,启动tomcat时会报错,而换成2.1.6的包,就没有报错了,具体原因还没搞明白,该插件包放在lib目录下。

5、给web.xml文件配置listener

加入了struts2-spring插件后,需要在web.xml文件中配置一个listener,该listener的作用是,在启动tomcat时启动Spring,配置了listener之后的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><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><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><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener></web-app>
6、编写JSP和对应的Action,这里举登录的例子,login.jsp中就是一个form表单,包含username和password加提交按钮,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>    <s:form action="login" method="post">    <s:textfield label="username" name="username"></s:textfield>    <s:password label="password"  name="password"></s:password>    <s:submit value="submit"></s:submit>    </s:form>  </body></html>
跳转到对应的Action的代码如下所示:

package com.test.action;import com.opensymphony.xwork2.ActionSupport;import com.test.service.LoginService;public class LoginAction extends ActionSupport {private String username;private String password;private LoginService loginService;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public LoginService getLoginService() {return loginService;}public void setLoginService(LoginService loginService) {this.loginService = loginService;}@Overridepublic String execute() throws Exception {if(loginService.checkLogin(username, password)){return SUCCESS;}return INPUT;}}
上面的Action中除了包含username和password两个属性外,还包含一个LoginService类,该类是用来处理登录校验的类,实际上LoginService是一个接口,接口中定义了检查登录是否合法的方法,具体实现放在了LoginServiceImpl类中了,下面是LoginService类和LoginServiceImpl类的代码:

package com.test.service;public interface LoginService {boolean checkLogin(String username, String password);}

package com.test.service.impl;import com.test.service.LoginService;public class LoginServiceImpl implements LoginService {@Overridepublic boolean checkLogin(String username, String password) {if("hello".equals(username) && "world".equals(password)){return true;}return false;}}
在LoginServiceImpl中,只是简单的判断了如果username为"hello"且password为"world",就代表登录成功,否则登录失败

7、配置Spring的applicationContext.xml文件

由于有了Spring的存在,所以要在配置文件中加入bean标签,这里主要是LoginServiceImpl类和LoginAction对应的标签,applicationContext.xml文件的内容如下所示:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="loginService" class="com.test.service.impl.LoginServiceImpl" scope="singleton"></bean><bean id="loginAction" class="com.test.action.LoginAction" scope="prototype"><property name="loginService" ref="loginService"></property></bean></beans>
上面的配置文件里,主要配置了两个类对应的bean,并将LoginServiceImpl类对应的bean作为参数,传给LoginAction对应的bean,scope属性指明了类的类型,singleton表示该类是单例类,prototype代表每次请求LoginAction时都会创建一个LoginAction类。

8、配置struts.xml文件

这里的struts.xml文件与以往写的略有不同,由于加入了Spring的支持,所以struts.xml中的action标签里,class不再写类的全名了,而是用applicationContext.xml中类指定的id来代替,下面是struts.xml文件的内容,注意class没有写类的全名了,而是用applicationContext.xml中定义的id  LoginAction来代替:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" ><struts><package name="example" extends="struts-default"> <action name="login" class="loginAction"><result name="success">/loginSuccess.jsp</result><result name="input">/login.jsp</result></action></package></struts>
到这里基本上一个加入Struts2和Spring支持的web项目就写好了,在浏览器中访问login.jsp页面效果如下:




输入hello和world后提交,出现Login Success,证明Struts2和Spring的整合工作是正确的

源码下载点击这里




0 0