本节主讲将struts与spring进行整合:
1,新建一个WEB工程:工程名为strutsspring,在其src目录上新建struts.xml配置文件
将struts相关的7个JAR包,拷至工程的WebRoot/Web-inf/lib目录下
2,配置 Web.xml文件,加上struts的过滤
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
经过以上两步配置,此工程已是标准的struts工程。
修改Tomcat/conf/server.xml配置文件,加上
<Context path="/strutsspring" docBase="E:\workspace\strutsspring\WebRoot" reloadable="true"/>
3,将spring相关的JAR包导入工程,将applicationContext.xml文件放入工程的web-inf目录下
4,将struts与spring整合:
将struts lib目录下的struts2-spring-plugin-2.0.14.jar包复制到工程中,此包中只有一个class文件。
修改web.xml文件,加入监听器,如下:
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
启动tomcat服务器,启用完成。
例子:下面用一个登录的例子,结合struts与spring开发
登录页面login.jsp, 要求输入用户名与密码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix ="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
<s:form action="login">
<s:textfield name="username" size="20" label="username"></s:textfield>
<s:password name="password" size="20" label="password"></s:password>
<s:submit></s:submit>
</s:form>
</body>
</html>
定义一个接口,isLogin方法用于判断用户名与密码是否正确
package com.test.service;
public interface LoginService {
public boolean isLogin(String username,String password);
}
实现类,当username为hello,password为world时,返回true,否则返回false
package com.test.service.impl;
import com.test.service.LoginService;
/*
* 该类完成业务处理,向外暴露的是LoginService接口
*/
public class LoginServiceImpl implements LoginService {
public boolean isLogin(String username, String password) {
if("hello".equals(username) && "world".equals(password)){
return true;
}
return false;
}
}
action类中不处理业务逻辑,逻辑由LoginService 接口负责,其对象由spring负责生成
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 LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public void setPassword(String password) {
this.password = password;
}
//下面这个loginService由spring生成
public String execute() throws Exception {
if(loginService.isLogin(username, password)){
return "success";
}else{
return "error";
}
}
}
struts配置文件,此文件中不再负责生成action,改为由spring配置文件生成,class对应的值,必须对应spring配置文件中的bean id
<?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="strutsspring" extends="struts-default"> <!--下面这个class中的loginAction对应的是spring配置文件applicationContext.xml文件中声明的loginAction bean --> <!--现在由spring负责生成action--> <action name="login" class="loginAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
spring配置文件,负责生成action及loginService对象
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<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>
其中scope配置是重点!其范围如下图所示
一般无状态类应使用singleton,而action则建议使用request或prototype
无状态类是指只有方法,没有属性的类。