struts2与spring的整合详解

      package org.lmxzz.struts2.service;

public interface LoginService
{
 public boolean doLogin(String userName, String password) ;
}
再编写实现类LoginServiceImpl,具体代码如下:  
package org.lmxzz.struts2.service.impl;

import org.lmxzz.struts2.service.LoginService;

public class LoginServiceImpl implements LoginService
{
 public boolean doLogin(String userName, String password)
 {
  boolean flag = false ;
  
  if("LmxZz".equals(userName) && "3348635".equals(password))
   flag = true ;
  
  return flag ;
 }
}
接着是LoginAction.java的具体代码:

package org.lmxzz.struts2.action;

import org.lmxzz.struts2.service.LoginService;

import com.opensymphony.xwork2.ActionSupport;

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 void setLoginService(LoginService loginService)
 {
  this.loginService = loginService;
 }
 
 @Override
 public String execute() throws Exception
 {
  if(loginService.doLogin(userName, password))  
   return SUCCESS ;  
  else
   return INPUT ;
 }
}
接着,修改index.jsp文件,修改后代码如下:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
    <title>ndex.jsp</title>
  </head>
 
  <body>
   <s:form action="login">
    <s:textfield name="userName" label="userName"/>
    <s:textfield name="password" label="password"/>
    <s:submit/>
   </s:form>
  </body>
</html>


这里需要注意的是<s:form action="login"> 中的 login,具体要注意什么在struts.xml里再进行说明

下面是重要的struts.xml 和 applicationContext.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.objectFactory" value="spring"/>
  <package name="struts2" extends="struts-default">
   <action name="login" class="loginAction">
    <result>success.jsp</result>
    <result name="input">index.jsp</result>
   </action>
  </package>
 </struts>


下面的是applicationContext.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 <bean id="loginService" class="org.lmxzz.struts2.service.impl.LoginServiceImpl"/>
 
 <bean id="loginAction" class="org.lmxzz.struts2.action.LoginAction" scope="prototype">
  <property name="loginService">
   <ref bean="loginService"/>
  </property>
 </bean>
</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 的代码如下:

<?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">
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

这里最需要注意的是:

 <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页面去了~~

该简单的例子也就完成了·~~~

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值