博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

IT民工窝棚

有意思的事很多,有多少有意义的呢?
   qbq.cublog.cn
关于作者  
姓名:QBQ
职业:PG
年龄:25
位置:DL
个性介绍:有意思的事很多,有多少有意义的呢?

我的分类  




Struts2+Spring+Hibernate整合入门详解(2)

有了两个头,又有了保持内容的类,现在看看我们如何用struts把他们联系起来吧。

现在需要在WEB-INF下建立文件struts-config.xml。其中form-beans定义了表单是如何映射的,这里用我们刚刚定义的forms.UserInfoForm

<?xml version=1.0” encoding="ISO-8859-1"?>

<!DOCTYPE struts-config PUBLIC   "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> 

<struts-config>

    <form-beans>

       <form-bean name="userInfoForm" type="forms.UserInfoForm"/>

    </form-beans>    

    <action-mappings>

       <action attribute="userInfoForm" path="/login" input="/index.jsp" type="org.springframework.web.struts.DelegatingActionProxy"

              name="userInfoForm" scope="session" validate="false">

           <forward name="success" path="/success.html"/>

       </action>

    </action-mappings>

</struts-config>

<action-mappings>中定义了我们的Action。它的属性attribute指出Action的内容输入是我们自定义的ActionFormpathAction赋予一个路径,input指明只接受index.jsp的输入,<forward标签定义了当Action返回"success"的时候,将定向到/success.html这个网页。 最重要的是type,它定义了这个处理这个请求的Action类,本来应该是我们自定义的LoginAction,但我们却用了spring的一个Action,为什么?因为我们要用Spring管理我们自定义的Action。看,strutsSpring在这里就开始连接起来了。 

但还有两个问题,StrutsSpring又是如何知道对方的存在,如何沟通呢?Spring如何知道把控制权交给我们自定义的LoginAction呢?

我们先来解决第一个问题,web.xmlTomcat这些应用服务器管理的,因此我们在这里将strutsSpring配置联系起来。这是整个web.xml。请看注释。

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" id="WebApp"

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

<display-name> Struts2+Spring2+Hibernate3 simple example by Doer Liu@UTstarcom</display-name>

 <!-- filter就理解为一些对网页请求的过滤吧 -->

 <!-- encodingFilter是为了处理国际化,交由Spring处理,设置为UTF-8 -->

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

 <!-- struts strutsfilter,这个定义就将可以将请求交给struts过滤一番了 -->

 <filter>

 <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

 </filter> 

<!-- 那么哪些请求交给struts过滤呢,这里包括 /struts2spring2hib3bydoer下和根目录/下的所有请求-->

 <filter-mapping>

 <filter-name>struts</filter-name>

 <url-pattern>/struts2spring2hib3bydoer/*</url-pattern>

 <url-pattern>/*</url-pattern>

 </filter-mapping>

 <!-- 定义一个监听器,处理整个WebContext,简单的理解为整个网站的上下文环境监听器吧 这个属于Spring-->

 <listener>

  <listener-class>

   org.springframework.web.context.ContextLoaderListener

  </listener-class>

 </listener> 

<!-- servlet定义一个servletstrutsActionServlet -->

    <servlet>

       <servlet-name>doertest</servlet-name>

       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

       <load-on-startup>1</load-on-startup>

    </servlet> 

<!-- servlet-mappingservlet和请求对应起来,这里是所有*.do的请求交由上面定义的doertest处理 -->

    <servlet-mapping>

       <servlet-name>doertest</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping> 

<!-- 定义默认返回页,如输入http://127.0.0.1/那么根目录下的index.html或者其他文件就被请求 -->

    <welcome-file-list>

       <welcome-file>index.html</welcome-file>

       <welcome-file>index.htm</welcome-file>

       <welcome-file>index.jsp</welcome-file>

       <welcome-file>default.html</welcome-file>

       <welcome-file>default.htm</welcome-file>

       <welcome-file>default.jsp</welcome-file>

    </welcome-file-list>

</web-app> 

通过web.xml两者联系上了。现在它们各自还需要一些配置。

Struts在我们的例子里比较简单,在build/class下面(最终会被eclipse同步到网站的WEB-INF/classes下面)建立struts.xml

<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd"> 

<struts>

    <include file="struts-default.xml" />

</struts>

Spring的默认配置文件是WEB-INF/applicationContext.xml,目前其内容很简单,我们只是把strutsBean放进来,如下:

映射的规则:beanname属性必须等于struts-config.xml里面定义的actionpath属性,class就是这个bean的类action.LoginAction

<?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">