struts2与spring整合示例

在昨天的struts2(v2.2.1)第一个入门示例的基础上,参考网上的一些资源,增加了spring(v3.1)整合,整合步骤如下:

1.引人相关的包:在昨天引入的jar包基础上,strtus2还要引入struts2-spring-plugin包,spring需要导入的包有:core,web,context,beans,asm,expression,为了使用spring dao我加入了jdbc包,留着以后用,呵呵

2.在引入包之后,在web.xml里配置spring配置文件的位置,以及用来加载spring配置文件的ContextLoaderListener

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
	
  <!-- 加载struts2核心 -->
  <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>
  <!-- 指明spring配置文件在何处 -->
  <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml,classpath*:applicationContext*.xml</param-value>
  </context-param>
   <!--  加载spring配置文件applicationContext.xml  --> 
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.配置struts.xml

<?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>
   <constant name="struts.i18n.encoding" value="utf-8"></constant>
   //看这里
   <constant name="struts.objectFactory" value="spring"></constant>
   
   
	<package name="strutsqs"  extends="struts-default">
		<action name="Login" class="com.struts2.action.LoginAction">
			<result name="success">/welcome.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

4.为了在struts中体现spring的功能,将以前在action里处理的用户验证放入Service里去,新建LoginSerive.java文件如下

package com.struts2.service;

public class LoginService {
	public boolean validate(String username,String password) throws Exception{
		boolean flag=false;
		if(username.equals("tom")&&password.equals("111111")){
			flag=true;
		}
		return flag;
	}
}

5.为了使用Service,修改LoginAction.java代码如下

package com.struts2.action;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.struts2.service.LoginService;

public class LoginAction extends ActionSupport implements Action{
	private static final long serialVersionUID  =   1L ;
	private String username;
	private String password;
	
	private LoginService loginService;
	
	@Override
	public void validate()
	{
		
	}
	
	
	public String execute() throws Exception
	{
		boolean result=this.loginService.validate(username, password);
		if(result){
			return this.SUCCESS;
		}else{
			return this.ERROR ;
		}
		
		
		
	}

	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;
	}
	
	
	
}

6.最后配置spring的配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE beans PUBLIC    
    "-//SPRING//DTD BEAN//EN"   
    "http://www.springframework.org/dtd/spring-beans.dtd">  
<beans default-autowire="autodetect">  
	<bean id="loginService" class="com.struts2.service.LoginService"></bean>
    <bean id="hello" class="com.struts2.action.LoginAction">
    	<property name="loginService" ref="loginService"></property>
    </bean>  
</beans>  

你可能感兴趣的:(struts2与spring整合示例)