Struts与Velocity的简单集成

首先当然是简单的配置web.xml与struts-config.xml文件,加入必要的servlet等东西:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
  <servlet>
    <servlet-name>velocity</servlet-name>
    <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
  </servlet>  
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.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>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</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>
  
  
  <welcome-file-list>
    <welcome-file>vms/login.vm</welcome-file>
  </welcome-file-list>
</web-app>

Struts-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="loginForm" type="com.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="com.jie.struts.action.LoginAction" />

  </action-mappings>

  <message-resources parameter="com.jie.struts.ApplicationResources" />
</struts-config>

然后是一个用于测试的登录页面:login.vm

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录页面</title>
<style type="text/css">
<!--
.STYLE1 {color: #00FF33}
-->
<!--
.STYLE2 {color: red}
-->
</style>
</head>

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

还有一个后台处理的LoginAction.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("123"))
                {                                        
                        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");
                
        }

相关推荐