参考:
http://www.blogjava.net/oksonic/archive/2007/03/01/101252.html
功能描述:
在test.jsp页面输入信息,点击提交按钮,有test action获得testform中的信息输出到test.vm中。。
预备工作:
Velocit1.5.jar
velocity-tools-1.3.jar
1. 建立 VelocityWeb web工程
2.web.xml 配置文件信息
3. struts-config.xml配置文件信息
4. Testaction
7. test.vm
功能描述:
在test.jsp页面输入信息,点击提交按钮,有test action获得testform中的信息输出到test.vm中。。
预备工作:
Velocit1.5.jar
velocity-tools-1.3.jar
1. 建立 VelocityWeb web工程
2.web.xml 配置文件信息
- <?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>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>velocity</servlet-name >
- <url-pattern >*.vm</url-pattern >
- </servlet-mapping >
- </web-app>
3. struts-config.xml配置文件信息
- <?xml version="1.0" encoding="UTF-8"?>
- <!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="testForm" type="com.yourcompany.struts.form.TestForm" />
- </form-beans>
- <action-mappings >
- <action
- attribute="testForm"
- input="/test.jsp"
- name="testForm"
- path="/test"
- scope="request"
- type="com.yourcompany.struts.action.TestAction" >
- <forward name="success" path="/test.vm"></forward>
- </action>
- </action-mappings>
- </struts-config>
4. Testaction
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- TestForm testForm = (TestForm) form;
- request.setAttribute("test", testForm);
- return mapping.findForward("success");
- }
5. TestForm
只包含一个属性 String test;
6. test.jsp
- <html:form action="/test">
- test : <html:text property="test"/><html:errors property="test"/><br/>
- <html:submit/><html:cancel/>
- </html:form>
7. test.vm
- <body>
- ${test.getTest()}
- </body>
运行结果:
http://127.0.0.1:8080/VelocityWeb/test.jsp
输入 :Hello VerRan
转向
http://127.0.0.1:8080/VelocityWeb/test.do
输出结果:Hello VerRan
如果直接访问:
http://127.0.0.1:8080/VelocityWeb/test.vm
输出:${test.getTest()}