Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29943645
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-05-19 11:43:47

有了两个头,又有了保持内容的类,现在看看我们如何用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"          ""> 

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

    xmlns:xsi=""

    xsi:schemaLocation="

    /web-app_2_5.xsd">

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

 

 

 <filter>

 <filter-name>encodingFilterfilter-name>

 <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>

 <init-param>

 <param-name>encodingparam-name>

 <param-value>UTF-8param-value>

 init-param>

 filter>

 

 <filter>

 <filter-name>strutsfilter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>

 filter> 

 <filter-mapping>

 <filter-name>strutsfilter-name>

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

 <url-pattern>/*url-pattern>

 filter-mapping>

 

 <listener>

  <listener-class>

   org.springframework.web.context.ContextLoaderListener

  listener-class>

 listener> 

    <servlet>

       <servlet-name>doertestservlet-name>

       <servlet-class>org.apache.struts.action.ActionServletservlet-class>

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

    servlet> 

    <servlet-mapping>

       <servlet-name>doertestservlet-name>

        <url-pattern>*.dourl-pattern>

    servlet-mapping> 

    <welcome-file-list>

       <welcome-file>index.htmlwelcome-file>

       <welcome-file>index.htmwelcome-file>

       <welcome-file>index.jspwelcome-file>

       <welcome-file>default.htmlwelcome-file>

       <welcome-file>default.htmwelcome-file>

       <welcome-file>default.jspwelcome-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"

        ""> 

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

<beans>

 

  <bean name="/login" class="action.LoginAction " singleton="false">  

  property>

  bean>  

beans>

现在在WebContent下面建立success时重定向的目标success.html,方法和index.jsp类似,但选择THML类型,随便输入内容以便测试。这时候strutsSpring就简单的连接起来了。先停掉刚才运行起来的Tomcat,重新启动,运行index.jsp,点击网页中的按钮<添加>,看看有什么效果。

现在,然我们简略描述一下数据和请求的流程。

点击<添加>index.jsp的这个表单发送的请求是login.do(

),请求被传给后台,生成了doertest(处理*.do的请求)集合的一个servlet,然后传到path/loginaction,被Springorg.springframework.web.struts.DelegatingActionProxy处理,该类找到name/loginBean,转交处理权,等待结果。这个Bean就是我们的action.LoginAction。我们的execute中返回一个forward"success"对应的网页,就是success.html。所以……,你已经看到了,strutsspring已经联系起来了。OK 

阅读(2705) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~