spring4.1.6整合Struts2.5.5

来源:互联网 发布:关于朱成分分析的数据 编辑:程序博客网 时间:2024/05/23 17:17

spring4.1.6整合Struts2.5.5

前言
- 准备条件
- 导入必要的jar
- 编写web.xml文件
- 编写struts.xml文件
- 编写spring.xml文件
- 编写log4j2-test.properties文件
- 编写测试Aciton
- 编写jsp页面
- 发布到tomcat8.0服务器
- 启动服务器并访问对应的jsp页面
- 总结


前言

    本文是介绍整合struts2.2.5和spring4.1.6的文章。本文通过9个步骤搭建一个最小的struts2.2.5和spring4.1.6的框架。其中也有log4j2的配置。    本文需要编写的文件有web.xml, spring.xml, log4j-test.properties,struts.xml, UserAction.java, login.jsp6个文件。通过本文可以学会搭建struts和spring的最小框架。

准备条件

    struts2.2.5相关jar包,spring4.1.6相关jar包,tomcat8.0服务器和eclipse集成IDE,读者可自行到各大官网下载依赖的jar包

导入jar包

需要导入的jar包如下图所示:

这里写图片描述


编写web.xml文件

编写必要的web.xml配置文件
<?version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"         id="ssheWebApp" version="2.5">    <display-name>TestSystem</display-name>    <!-- 配置需要初始化的xml文件,不配置时默认加载applicatinContext.xml -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:spring.xml</param-value>    </context-param>    <!-- 配置struts2的核心拦截器,官方推出用StrutsPreparedAndExcuteFilter代替FilterDispatcher -->    <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPreparedAndExcuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- 配置spring的监听器,默认加载WEB-INF/applicationContext.xml,通过上面的contex-param标签配置之后,加载context-param中的子标签param-value指定的xml文件 -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 配置字符集拦截器,由spring提供 -->    <filter>        <filter-name>encodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- struts.xml不需要在本文件中指定加载,由框架自行加载 --></web-app>

编写struts.xml文件

编写struts.xml配置struts
<?version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    "http://struts.apache.org/dtds/struts-2.5.dtd"><struts>    <!-- 包含struts-default.xml文件 -->    <include file="struts-default.xml"></include>    <!-- 设置字符集为UTF-8 -->    <constant name="struts.i18n.encoding" value="UTF-8">    </constant>    <!-- 设置struts2允许的后缀名 -->    <constant name="struts.action.extension" value="do,action"></constant>    <!-- 是否允许浏览器缓存静态内容 -->    <constant name="struts.serve.static.browserCache" value="false"></constant>    <!-- 配置文件变化时是否重新加载 -->    <constant name="struts.configuration.xml.reload" value="true"></constant>    <!-- 是否开启开发者模式 -->    <constant name="struts.devMode" value="true"></constant>    <!-- 是否允许动态方法调用 -->    <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>    <!-- 由spring担任工程bean -->    <constant name="struts.ObjectFactory" value="spring"></constant>    <!-- 映射action -->    <package name="user" namespace="/" extends="struts-default">        <action name="hello" class="com.xxx.controller.UserAction" method="test">            <result name="index">index.jsp</result>        </action>    </package></struts>

编写spring.xml文件

    编写spring.xml配置系统需要的类容,这里加入了很多spring的头文件描述,读者可根据需要进行增减。
<?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:aop="http://www.springframework.org/schema/aop"     xmlns:c="http://www.springframework.org/schema/c"     xmlns:cache="http://www.springframework.org/schema/cache"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:jdbc="http://www.springframework.org/schema/jdbc"     xmlns:jee="http://www.springframework.org/schema/jee"     xmlns:lang="http://www.springframework.org/schema/lang"     xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:p="http://www.springframework.org/schema/p"     xmlns:task="http://www.springframework.org/schema/task"     xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:util="http://www.springframework.org/schema/util"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">    <!-- 开启注解编程 -->    <!-- <mvc:annotation-driven></mvc:annotation-driven> -->    <context:annotation-config></context:annotation-config>    <!-- 指定需要开启注解编程的包 -->    <context:component-scan base-package="com.youjile">    </context:component-scan></beans>

编写log4j2-test.properties文件

配置log4j2,如果读者想对log4j2进行其他配置,可以参考网上配置或者官方的配置。
log4j.rootLogger=ERROR,Console,OneFile,HtmlFilelog4j.logger.org.apache.cxf=DEBUGlog4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACElog4j.logger.org.hibernate.tool.hbm2ddl=DEBUG#log4j.logger.org.hibernate.SQL=DEBUG#log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=TRACElog4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.Target=System.outlog4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.Threshold=ALLlog4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%nlog4j.appender.OneFile=org.apache.log4j.RollingFileAppenderlog4j.appender.OneFile.File=d:/OASystem/oasys.txtlog4j.appender.OneFile.MaxFileSize=10MBlog4j.appender.OneFile.Threshold=ERRORlog4j.appender.OneFile.layout=org.apache.log4j.PatternLayoutlog4j.appender.OneFile.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%nlog4j.appender.HtmlFile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.HtmlFile.file=../logs/oasys/oasyslog4j.appender.HtmlFile.DatePattern='_'yyyy-MM-dd'.html'log4j.appender.HtmlFile.layout=org.apache.log4j.HTMLLayoutlog4j.appender.HtmlFile.Threshold=ERROR 

编写测试UserAciton类

编写用于测试的Action类。
/* 这里是控制层action */public class UserAction{    private String username;    private String password;    /* 控制层处理逻辑 */    public String test(){        System.out.println(this.username+"--"+this.password);    }    public void setUsername(String username){        this.username = username;     }    public  String getUsername(){        return username;    }    public void setPassword(String password){        this.password = password;    }       public String getPassword(){        return password;    }}

login.jsp页面

编写login.jsp页面
/* 以下是login.jsp中的内容 */<body>    <form action="hello.do" method="post">        用户名:<input type="text" name="username">        密码:<input type="password" name="password">        <input type="submit" value="登录">    </form></body>

发布到tomcat8.0服务器

将工程编译之后发布到tomcat8.0服务器

启动服务器并访问login.jsp页面

启动服务器如下图所示:

这里写图片描述

在浏览器中输入如下地址:

这里写图片描述

    输入用户名和密码之后点击提交。如果控制台打印出用户名和密码,说明配置成功。

总结

    通过本文可以了解怎么搭建一个最小的struts+spring框架。本文采用的jar文件都是在struts、spring和log4j的官方网站下载的,如有不懂也可参照各个官方网站的列子进行设计。本文跟官方网站冲突的类容以官方网站为准。    最后,由于本人知识水平和能力的局限,不免有错误之处,如有错误希望大家可以提出来,一起学习参考。
0 0
原创粉丝点击