知识大全 Struts与Velocity的简单集成

Posted

篇首语:等不到的晚安就别等了,挤不进的世界就别挤了。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Struts与Velocity的简单集成相关的知识,希望对你有一定的参考价值。

Struts与Velocity的简单集成  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  先当然是简单的配置web xml与struts config xml文件 加入必要的servlet等东西: Xml代码    <?xml version= encoding= UTF ?>    <web app xmlns= xmlns:xsi= instance version= xsi:schemaLocation= ;  app_ _ xsd >

  <servlet>        <servlet name>velocity</servlet name>        <servlet class> apache velocity tools view servlet VelocityViewServlet</servlet class>      </servlet>      <servlet>        <servlet name>action</servlet name>        <servlet class> apache struts action ActionServlet</servlet class>        <init param>          <param name>config</param name>          <param value>/WEB INF/struts config xml</param value>        </init param>        <init param>          <param name>debug</param name>          <param value> </param value>        </init param>        <init param>          <param name>detail</param name>          <param value> </param value>        </init param>        <load on startup> </load on startup>      </servlet>

  <servlet mapping>        <servlet name>velocity</servlet name>        <url pattern>* vm</url pattern>      </servlet mapping>      <servlet mapping>        <servlet name>action</servlet name>        <url pattern>* do</url pattern>      </servlet mapping>

  <wele file list>        <wele file>vms/login vm</wele file>      </wele file list>    </web app>

  Struts config xml:

  <?xml version= encoding= UTF ?>    <!DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >

  <struts config>      <data sources />      <form beans >        <form bean name= loginForm type= jie struts form LoginForm />

  </form beans>

  <global exceptions />      <global forwards >        <forward name= success path= /vms/success vm />        <forward name= fail path= /vms/fail vm />        <forward name= first path= /vms/login vm />

  </global forwards>

  <action mappings >        <action          attribute= loginForm           input= /login vm           name= loginForm           path= /login           scope= request           type= jie struts action LoginAction />

  </action mappings>

  <message resources parameter= jie struts ApplicationResources />    </struts config>    view plaincopy to clipboardprint?    <?xml version= encoding= UTF ?>    <web app xmlns= xmlns:xsi= instance version= xsi:schemaLocation= ;  app_ _ xsd >

  <servlet>        <servlet name>velocity</servlet name>        <servlet class> apache velocity tools view servlet VelocityViewServlet</servlet class>      </servlet>      <servlet>        <servlet name>action</servlet name>        <servlet class> apache struts action ActionServlet</servlet class>        <init param>          <param name>config</param name>          <param value>/WEB INF/struts config xml</param value>        </init param>        <init param>          <param name>debug</param name>          <param value> </param value>        </init param>        <init param>          <param name>detail</param name>          <param value> </param value>        </init param>        <load on startup> </load on startup>      </servlet>

  <servlet mapping>        <servlet name>velocity</servlet name>        <url pattern>* vm</url pattern>      </servlet mapping>      <servlet mapping>        <servlet name>action</servlet name>        <url pattern>* do</url pattern>      </servlet mapping>

  <wele file list>        <wele file>vms/login vm</wele file>      </wele file list>    </web app>

  Struts config xml:

  <?xml version= encoding= UTF ?>    <!DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >

  <struts config>      <data sources />      <form beans >        <form bean name= loginForm type= jie struts form LoginForm />

  </form beans>

  <global exceptions />      <global forwards >        <forward name= success path= /vms/success vm />        <forward name= fail path= /vms/fail vm />        <forward name= first path= /vms/login vm />

  </global forwards>

  <action mappings >        <action          attribute= loginForm           input= /login vm           name= loginForm           path= /login           scope= request           type= jie struts action LoginAction />

  </action mappings>

  <message resources parameter= jie struts ApplicationResources />    </struts config>

  <?xml version= encoding= UTF ?>    <web app xmlns= xmlns:xsi= instance version= xsi:schemaLocation= ;  app_ _ xsd >

  <servlet>        <servlet name>velocity</servlet name>        <servlet class> apache velocity tools view servlet VelocityViewServlet</servlet class>      </servlet>      <servlet>        <servlet name>action</servlet name>        <servlet class> apache struts action ActionServlet</servlet class>        <init param>          <param name>config</param name>          <param value>/WEB INF/struts config xml</param value>        </init param>        <init param>          <param name>debug</param name>          <param value> </param value>        </init param>        <init param>          <param name>detail</param name>          <param value> </param value>        </init param>        <load on startup> </load on startup>      </servlet>

  <servlet mapping>        <servlet name>velocity</servlet name>        <url pattern>* vm</url pattern>      </servlet mapping>      <servlet mapping>        <servlet name>action</servlet name>        <url pattern>* do</url pattern>      </servlet mapping>

  <wele file list>        <wele file>vms/login vm</wele file>      </wele file list>    </web app>

  Struts config xml:

  <?xml version= encoding= UTF ?>    <!DOCTYPE struts config PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN config_ _ dtd >

  <struts config>      <data sources />      <form beans >        <form bean name= loginForm type= jie struts form LoginForm />

  </form beans>

  <global exceptions />      <global forwards >        <forward name= success path= /vms/success vm />        <forward name= fail path= /vms/fail vm />        <forward name= first path= /vms/login vm />

  </global forwards>

  <action mappings >        <action          attribute= loginForm           input= /login vm           name= loginForm           path= /login           scope= request           type= jie struts action LoginAction />

  </action mappings>

  <message resources parameter= jie struts ApplicationResources />    </struts config>然后是一个用于测试的登录页面:login vm Html代码    < xmlns= >    <head>    <meta equiv= Content Type content= text/; charset=gb />    <title>登录页面</title>    <style type= text/css >    <!     STYLE color: # FF     >    <!     STYLE color: red    >    </style>    </head>

  <body>    <form id= form name= form method= post action= login do >      <label>用户名      <input type= text name= userName />      </label>      <p>        <label>密  码        <input type= password name= password />        </label>      </p>      <p>        <label>        <input type= submit name= Submit value= 提交 />        </label>      </p>    </form>    #if($isSuccess)<p class= STYLE >验证通过 </p>#else  #end    #if($isNull)<p class= STYLE >字段不能为空 </p>#else  #end    </body>    </>    view plaincopy to clipboardprint?    < xmlns= >    <head>    <meta equiv= Content Type content= text/; charset=gb />    <title>登录页面</title>    <style type= text/css >    <!     STYLE color: # FF     >    <!     STYLE color: red    >    </style>    </head>

  <body>    <form id= form name= form method= post action= login do >      <label>用户名      <input type= text name= userName />      </label>      <p>        <label>密  码        <input type= password name= password />        </label>      </p>      <p>        <label>        <input type= submit name= Submit value= 提交 />        </label>      </p>    </form>    #if($isSuccess)<p class= STYLE >验证通过 </p>#else  #end    #if($isNull)<p class= STYLE >字段不能为空 </p>#else  #end    </body>    </>

  < xmlns= >    <head>    <meta equiv= Content Type content= text/; charset=gb />    <title>登录页面</title>    <style type= text/css >    <!     STYLE color: # FF     >    <!     STYLE color: red    >    </style>    </head>

  <body>    <form id= form name= form method= post action= login do >      <label>用户名      <input type= text name= userName />      </label>      <p>        <label>密  码        <input type= password name= password />        </label>      </p>      <p>        <label>        <input type= submit name= Submit value= 提交 />        </label>      </p>    </form>    #if($isSuccess)<p class= STYLE >验证通过 </p>#else  #end    #if($isNull)<p class= STYLE >字段不能为空 </p>#else  #end    </body>    </>还有一个后台处理的LoginAction java: Java代码    public ActionForward execute(ActionMapping mapping ActionForm form                             HttpServletRequest request HttpServletResponse response)                     LoginForm loginForm = (LoginForm) form;// TODO Auto generated method stub                    String userName = loginForm getUserName();                    String password = loginForm getPassword();

  if(userName equals( jie ) && password equals( ))                                                isSuccess = true;                            request setAttribute( isSuccess isSuccess);//集成的关键点在这里 把对象加入到vm模板                            return mapping findForward( first );         里面 然后可以在模板里面用 $isSuccess 来使用

  else if(userName== || password== )                                                boolean isNull = true;                            request setAttribute( isNull isNull);                            return mapping findForward( first );                                        return mapping findForward( fail );

cha138/Article/program/Java/ky/201311/28282

相关参考

知识大全 深入探讨Spring与Struts 的集成方案

深入探讨Spring与Struts的集成方案  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Sp

知识大全 struts2与freemarker的集成

  将freemarker的jar包添加到项目中  创建Action类   Java代码   //此处的Configuration是free

知识大全 如何有效的集成Spring和Struts

如何有效的集成Spring和Struts  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  本文想通

知识大全 实例说明如何集成Spring和Struts

实例说明如何集成Spring和Struts  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  本文想

知识大全 集成 Struts、Tiles 和 JavaServer Faces

集成Struts、Tiles和JavaServerFaces  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看

知识大全 演示Struts2实现简单上传代码

演示Struts2实现简单上传代码  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  webxml&

知识大全 实现一个简单的struts和spring框架

  apache终于发布了struts的正式版strutsGA所以我也该从webwork迁移至strutsstruts基本上就是webwork的翻版所以迁移过程倒是很简单只需要修改下配置文件和一些包名

知识大全 Struts里一个简单好用的过滤器

Struts里一个简单好用的过滤器  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  网上过滤器的使

知识大全 Struts2 Spring Hibernate 的简单整合

Struts2SpringHibernate的简单整合  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 实例讲解Spring集成JSF的最简单方式

实例讲解Spring集成JSF的最简单方式  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Spr