Java EE之Spring+Struts2框架整合手册

struts优点

1>在软件设计上没有像struts1那样跟servlet API和struts API紧密耦合,不依赖他们,属于无侵入设计。

2>提供了拦截器,利用拦截器可进行AOP编程,实现权限拦截等功能。

3>提供类型转换器,可将特殊请求参数转换成需要类型。(struts1必须向struts1底层实现BeanUtil,注册类型转换器才行)

4>提供支持多种表现技术,如Jsp,freeMarker,Velocity等

5>转入校验可指定方法进行校验,解决了strust1长久之痛。

6>提供了全局范围,包范围和Action范围的国际化资源文件管理实现。(Webwork2基础发展而来,与struts1一样属于MVC框架)

struts2处理流程

1.strutsPrepareAndExecuteFilter是struts2框架的核心控制器,负责拦截由<url-patter>/*</url-patter>指定的所有用户请求,当用户请求到达后,改Filter会过滤用户请求。

2.默认情况下,用户请求的路径不带后缀或后缀以.action结尾,这时请求转入struts2框架处理,否则struts2框架将略过该请求处理。

3.当请求转入struts2框架处理时会先经过一系列拦截器Intercetor(struts2内置拦截器或用户自定义拦截器)到达action-->Result(类似于struts1中的forward)-->Jsp/html-->响应。

注意:与strust1不同,struts2对用户的每次请求都会创建一个action,所以struts2中的Action线程是安全的。

    struts1中Action和struts2中的Action在管理方式中有何不同?

    struts1采用单例模式,struts2采用原型模式   Scope[缓存中获取Action]

1.搭建环境

  • 导jar包
  • 编写struts2配置文件
  • web.xml中加入是struts2MVC框架启动和配置

2.Action名搜索顺序

  • namespace名找,不存在返回上一层目录寻找
  • 包不存在,则在默认命名空间中寻找action(即namespace为空或不设置)

3.Action配置的各项默认值

4.result配置的各种视图转发类型

  • result类似于forword  disoatcher(默认)类似于内部请求转发
  • redirect(浏览器重定向)  不能定义到WEB-INF下
  • redirectAction(同一包下直接写action名)

不同包下设置<param name> actionName及namespace或继承namespace

<global-result>全视图

传中文 URLEncoder.encode("中文","UTF-8");

Tomcat:ISO-8859-1 解码:java.net.URLDecoder包 request.getParameter().getBytes("","");

  • plainTXT原样显示源代码

<param name="location">路径</param>

<param name="charSet">UTF-8</param>指定文件读取编码

5.struts2.xml

(1)为属性注入值  在<action>中<param "属性名"> 值 </param>

(2)常量定义 constant <constant name="" value=""/>

Spring+Struts2框架整合手册

1.工程结构图

2.配置文件

2.1 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">

    <!-- spring -->
    <!-- 加载spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 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>

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

2.2.1 struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <constant name="struts.custom.i18n.resources" value="emp"></constant>
    <constant name="struts.serve.static.browserCache" value="false"></constant>
    <constant name="struts.configuration.xml.reload" value="true"></constant>
    <constant name="struts.multipart.maxSize" value="10701096"></constant>
    <include file="struts-user.xml"/>
</struts>

2.2.2 struts-user.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <package name="user" namespace="/" extends="struts-default">
    <!-- 用户管理 -->
    <action name="userAction_*" class="UserAction" method="{1}">
      <!-- 登录失败停留在登录界面 -->
      <result name="failLogin">/index.jsp</result>
      <!-- 登录成功跳转至登陆界面 -->
      <result name="successLogin">/WEB-INF/login.jsp</result>
    </action>
  </package>
</struts>

2.3 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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 启用注解 -->
    <context:annotation-config />

    <!-- 扫描注解 -->
    <context:component-scan base-package="com.sinosoft.spring" />

</beans>

 

3.简单实现一个登陆操作(java源文件只提供主要业务实现逻辑,其他文件请自行建立)

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>欢迎来到中科软</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">
    -->
<title>登录界面</title>
<style>
body {
    text-align: center
}

.table {
    text-align: center;
}

h1 {
    font-size: 40px;
    text-align: center;
}
</style>
</head>
<body>
    <div
        style="border:1px solid #666;width:500px;height:80px;background-color:grey;">
        <h1>理赔业务系统</h1>
        <div class="table">
            <form action="userAction_login">
                用户名:<input type="text" id="username" name="user.userName" /><br>
                &nbsp;&nbsp;密码:<input type="password" id="password"
                    name="user.password" /><br>
                    <font size="2" color="red">${state }</font><br>
                <input type="checkbox" value="1" />记住密码
                <input type="submit" value="登录" />
            </form>
            <p style="font-size:12px">
                还没有账号?<a href="personAction_toSave"><b>立即注册</b>
                </a>
            </p>
        </div>
    </div>
</body>
</html>

UserAction.java

package com.sinosoft.spring.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sinosoft.spring.dao.User;
import com.sinosoft.spring.service.UserService;

/**
 * 用户管理执行层
 * @author sinosoft
 *
 */
@Component("UserAction")
@Scope("prototype")
public class UserAction {
    /** 登录方法 */
    public String login() {
        
        // mantis号 add by sinosoft start 2018/1/1
        int result = userService.findUserForLogin(user);
        // 登录成功
        if(result>0) {
            return "successLogin";
        // 登录失败
        }else {
            this.setState("用户名或密码错误!");
            return "failLogin";
        }
        // mantis号 end
    }
}

UserServiceImpl.java

package com.sinosoft.spring.service.impl;

import org.springframework.stereotype.Service;

import com.sinosoft.spring.dao.User;
import com.sinosoft.spring.service.UserService;

@Service
public class UserServiceImpl implements UserService{

    public int findUserForLogin(User user) {
        if (user != null 
                && "13000002".equals(user.getUserName())
                && "0000".equals(user.getPassword())) {
            return 1;
        } else {
            return 0;
        }    
    }
}

 

posted @ 2018-02-28 16:10  jxtx92  阅读(266)  评论(0)    收藏  举报