SSH框架搭建

闲来无事,来搞下SSH框架的搭建吧。


1.struts搭建

    这个不用说了吧,新建一个web项目,把struts-2.3.7-all解压后的struts2-blank里面的lib以下的包所有拷贝到你新建的web项目里面,把web-inf以下的web.xml复制过去,把src文件夹下的struts.xml拷贝到新建的src文件夹下。最重要的是你要集成spring,那么问题来了,你少包了,亲。去struts-2.3.7-all文件夹的lib文件夹下把struts2-spring-plugin-2.3.7包拷贝到新建的lib文件夹以下。调试,ok。


2.集成spring

    (1)aspectj目录以下的包都拷贝过去

AspectJ是一个面向切面的框架,它扩展了Java语言。spring 使用AOP的时候也会用到它。

(2)spring的核心包肯定少不了了。


  (3)cglib包也是必须的。

cglib是一个强大的,高性能,高质量的Code生成类库,它能够在执行期扩展Java类与实现Java接口。Hibernate用它来实现PO(Persistent Object 持久化对象)字节码的动态生成。

(4)j2ee目录下的common-annotations也须要

annotation是注解时须要用到。

(5)jakarta-commons目录下的commons-logging,commons-pool(java数据库连接池 包

(6)须要注意的是spring的jar包里面有slf的api和slf-log4j的转换包,那么还须要一个log4j的包,也就是总共三个

slf4j-api-1.5.0,slf4j-log4j12-1.5.0和log4j12-1.5.0这样输出log就不会报log什么的错误了。

ok上图。


眼下的jar包就这么多。

光说不练不行,那么我们先建立晒晒struts.xml的内容。

<span style="color:#333333;"><?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.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    

    <package name="default" namespace="/pages" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="userAction" class="</span><strong><span style="color:#ff0000;">userActionBean</span></strong><span style="color:#333333;">">
            <result name="success">success.jsp</result>
        </action>
    </package>

</struts></span>

好,大家看到粗红体的字了吗?为什么不是类的包路径呢???!!!对了,由于action也要交给spring容器实例化的啊。所以,这里仅仅写spring配置文件中的bean的name/id,以下是spring的配置文件applicationContext.xml的内容

<span style="color:#333333;"><?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       </span><span style="color:#ff0000;"><strong>xmlns:context="http://www.springframework.org/schema/context"</strong></span><span style="color:#333333;">      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- Resource注解方式得到实例化的bean -->
</span><strong><span style="color:#ff0000;">  <context:annotation-config/></span></strong><span style="color:#333333;">

  <bean id="userService" class="com.test.xuehf.serivce.UserServiceImp"/>
  <bean id="userActionBean" class="com.test.xuehf.action.UserAction">
  <!-- setter 方法注入  action中必需要有setter方法
  	<property name="userService" ref="userService"></property> -->
  </bean>


</beans></span>

怎么又有粗红体了!嗯,不要着急,这个是为了方便你在action中调用service的时候直接以@Resource注解的方式获取spring已经实例化的service实例而配置的,必需要写上这两句。OK,执行下吧

Caused by: java.lang.NullPointerException

oh,NO!why,为什么会报空指针异常呢?这是由于你的web.xml没有配置spring的监听,须要spring去初始化bean才不会有空指针的出错啊。

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>
    <!-- 	 -->
	<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>
	
    <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.html</welcome-file>
    </welcome-file-list>

</web-app>

那么ok了,如今为止spring集成struts已经能够执行了

未完待续。。。

原文地址:https://www.cnblogs.com/mfrbuaa/p/4490877.html