我们知道struts1与spring整合是靠org.springframework.web.struts.DelegatingActionProxy来实现的,以下通过具体一个用户登录实现来说明struts2整合spring的相关内容.

一、准备工作

1.实例分析我们在这不与数据库打交道,所有就是当用登录的时候判断用户名是否为指定值,密码是否为指定值,以及相关的异常处理、
        2.为什么我们要说struts2整合spring呢?相信在家都知道,我也不用多说了....
        3.在  http://struts.apache.org/download.cgi#struts212下载struts2的jar包,源码,API文档.
        4.在  http://static.springframework.org/downloads/nightly/release-download.php下载不同版本的spring的jar包,源码,API文档.
        5.配置开发环境:MyEclipse6.0+Eclipse3.3+JDK6.0+Tomcat6.0+Struts 2.0.11+spring2.0。
      6.新建web项目,导入相应的jar包,如以下所示:
            a.由于现在IDE开发工具还没有对struts2.0有很好的支持,所有我们需要手功配置,首先将我们刚下下来的struts2.0的lib里面的commons-logging-1.0.4.jar、ognl-2.6.11.jar、xwork-2.0.4.jar、freemarker-2.3.8.jar、struts2-core-2.0.11.1.jar以及struts2.0里面所需要的插件包struts2-spring-plugin-2.0.11.1.jar添加的WEB-INF/lib下面
            b.通过通过IDE开发工具项目对spring2.0的支持
        7.在src下建立struts.xml文件(具体的写法在后面呈现)
        8.配置web.xml,如下:

<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.FilterDispatcher
        </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>classpath*:applicationContext.xml</param-value>
    </context-param>

<!-- 加载spring配置文件applicationContext.xml -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>    
</web-app>

二、建立前台页面

1.用户登录肯定有一个用户登录页面login.jsp,如下:

<%@ taglib prefix="s"  uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
  <head>
      <title>login2</title>
  </head>

<body>
      <s:form name="login" action="login" method="post" >
          <s:textfield name="username" label="帐号"></s:textfield>
          <s:password name="password"  label="密码"></s:password>
          <s:submit></s:submit>
      </s:form>
  </body>
</html>

 

2.若登录成功的index.jsp文件,如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//CN">
<html>
  <head>
      <title>login2</title>
  </head>
    
  <body>
          <div>
              <h4>欢迎你!</h4><font color="red">${username}</font>
              <br/>
              <h4>你登录的密码是<h4><font color="red">${password}</font>;
          </div>
  </body>
</html>
 

3、用户名非法提示页面.usernameInvalid.jsp(通过el表达示得到异常信息)

<%@ page language="java" contentType="text/html; charset=GB18030"   
 pageEncoding="GB18030"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
    用户名非法:<font color="red">${exception.message}</font>
</body>
</html>

 

4、密码非法提示页面.passwordInvalid.jsp(struts2标签得到异常信息);

<%@ page language="java" contentType="text/html; charset=GB18030"

pageEncoding="GB18030"%>

 <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
    密码非法:<FONT  color="red"><s:property value="exception.message"/></FONT>
</body>
</html>

三、建立对应的action

1.提供用户请求服务的LoginService类

package org.topCSA.s2s.service;

import org.topCSA.s2s.exception.PasswordException;
import org.topCSA.s2s.exception.UsernameException;

public class LoginService
{
    /*
     * 我们这只是一个小的例子,不与数据库打交到
     * 若有数据库操作,那么在这个LoginService就是操作具体Dao类实现登录的相关操作
     */
    public boolean validate(String username,String password)throws Exception
    {
        boolean v = false;
        if(!"xcp".equals(username))//如果用户名不等于xcp,就抛出一个异常
        {
            throw new UsernameException("用户名不正确");
        }
        else if(!"123".equals(password))))//如果密码不等于123,就抛出一个异常

{
            throw new PasswordException("密码不正确");
        }
        else
        {
            v = true;            
        }
        return v;
    }
}

2.接收用户请求的LoginAction类

package org.topCSA.s2s.action;

import org.topCSA.s2s.service.LoginService;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{

/**
     * 
     */
    private static final long serialVersionUID = 1L;

private String username;
    private String password;
    
    /*
     * 我们通过Spring的IOC容器注入LoginService,从而减少类之间的依赖关系
     */
    private LoginService loginService;
    
    public LoginService getLoginService()
    {
        return loginService;
    }
    public void setLoginService(LoginService loginService)
    {
        this.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;
    }
    
    
    @Override
    public void validate()
    {
        /*
         * 我们可以在这个方法类加一些输入验证 但是为了体现后面我们写的业务逻辑方法这就不验证
         */
    }
    
    @Override
    public String execute() throws Exception
    {
        
        boolean result = loginService.validate(username, password);
        if(result == true)
        {
            return SUCCESS;
        }
        else
        {
            return INPUT;
        }
    }
}

四、配置struts.xml与applicationContext.xml
    
        1.配置struts.properties,为了解决中文问题,具体用法参照struts2的用法如下:里面加上struts.i18n.encoding = gb2312,当然也可以直接加到struts.xml里面写法为<constant name="struts.i18n.encoding" value="gbk"></constant>
        2.配置struts.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="struts2" extends="struts-default">
        <action name="login" class="LoginAction">
            <exception-mapping result="usernameInvalid" exception="org.topCSA.s2s.exception.UsernameException" />
            <exception-mapping result="passwordInvalid" exception="org.topCSA.s2s.exception.PasswordException" />
            <result name="success">/index.jsp</result>
            <result name="input">/login.jsp</result>
            <result name="usernameInvalid">/usernameInvalid.jsp</result>
            <result name="passwordInvalid">/passwordInvalid.jsp</result>
        </action>
    </package>
</struts>

3.配置applicationContext.xml

<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 name="loginService" class="org.topCSA.s2s.service.LoginService" />
    
    <bean name="LoginAction" class="org.topCSA.s2s.action.LoginAction">
        <property name="loginService">
            <ref bean="loginService"/>
        </property>
    </bean>

</beans>

五、测试(全部成功)    
    

struts2整合spring应用实例的更多相关文章

  1. struts2整合spring的思路

    struts2整合spring有有两种策略: >sping容器负责管理控制器Action,并利用依赖注入为控制器注入业务逻辑组件. >利用spring的自动装配,Action将自动会从Sp ...

  2. struts2整合spring出现的Unable to instantiate Action异常

    在struts2整合spring的时候,完全一步步按照官方文档上去做的,最后发现出现 Unable to instantiate Action,网上一搜发现很多人和我一样的问题,配置什么都没有错误,就 ...

  3. Struts2—整合Spring

    Struts2—整合Spring Spring框架是一个非常优秀的轻量级java EE容器,大部分javaEE应用,都会考虑使用Spring容器来管理应用中的组件. Struts2是一个MVC框架,是 ...

  4. Struts2整合Spring方法及原理

    一.   Struts 2框架整合Spring步骤 1. 复制文件.复制struts2-spring-plugin-x-x-x.jar和spring.jar到WEB-INF/lib目录下.其中的x对应 ...

  5. Struts2 整合Spring(Maven,注解版)

    这两天正在试验Struts2与Spring框架的整合,和他们各自的“注解”.今天就总结一下这两个框架怎么用注解进行整合. 一,加入两者的依赖包,除了两者的必要依赖外,还需要导入struts2-spri ...

  6. Echache整合Spring缓存实例讲解(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要介绍了EhCache,并通过整合Spring给出了一个使用实例. 一.EhCac ...

  7. Echache整合Spring缓存实例解说

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要介绍了EhCache,并通过整合Spring给出了一个使用实例. 一.EhCac ...

  8. Echache整合Spring缓存实例讲解

    摘要:本文主要介绍了EhCache,并通过整合Spring给出了一个使用实例. 一.EhCache 介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中 ...

  9. Struts2+Hibernate+Spring(SSH)三大框架整合jar包

    Struts2 + Spring3 + Hibernate3 框架整合 1. 每个框架使用 (开发环境搭建 )* 表现层框架 struts2 1) jar包导入: apps/struts2_blank ...

随机推荐

  1. android CTS测试

    CTS认证是获得Google推出的Android系统中Android Market服务的前提 CTS兼容性测试的主要目的和意义在于使得用户在Android系统的应用过程中,有更好的用户体验,并展现出A ...

  2. Xcode8.2 继续使用插件

    网上参考了文章:http://www.jianshu.com/p/ab819babf2c3 使用的是:update_xcode_plugins . 但要注意的是,在Xcode 8.2下安装,并没有给我 ...

  3. 笔记整理--Linux守护进程

    Linux多进程开发(三)进程创建之守护进程的学习 - _Liang_Happy_Life__Dream - 51CTO技术博客 - Google Chrome (2013/10/11 16:48:2 ...

  4. python+appium使用记录

    最近在研究appium+appiumlibrary移动端的两个自动化测试库,特此将使用过程,粗略记录一下 1.环境搭建,略,自行百度. 2.查看apk包名及activity方法,自行百度. 3.基本步 ...

  5. VBS 读取文本文件特殊字符前如逗号的值并赋值给变量

    我使用的仿真终端SecureCRT需要使用一个脚本,支持VBS的.我需要实现如下功能: 首先文本文件在:D:\100.txt文本文件的内容为:9 0,randy,9 1,jeff,9 2,sameul ...

  6. hdu_1536_S-Nim(DFS_SG博弈)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1536 题意:首先输入K ,表示一个集合的大小 , 之后输入集合, 表示对于这对石子只能去这个集合中的元 ...

  7. php - preg_match

    任务:匹配一个函数名或者变量名,如果碰到alpha,numeric,_以外的全部不允许通过. 实验1: <?php //第一个字符不符合就直接退出正则匹配 $str = '%abcscript% ...

  8. iOS中date和string的相互转换

    必须知道的内容 G: 公元时代,例如AD公元     yy: 年的后2位     yyyy: 完整年     MM: 月,显示为1-12     MMM: 月,显示为英文月份简写,如 Jan      ...

  9. css float 布局

    .clearfix:after { content: ''; display: table; clear: both; } .clearfix {; }

  10. JSP基本语法--实例演练

    基本语法概括:<%@page>,<%@include>,<jsp:include>,<jsp:forward> 加上数据库操作,可以开发动态web了. ...