架构搭建系统(二)Struts2+Velocity(带Velocity Tools2.0) 架构搭建全过程

我们在struts2.3基础架构上增加Velocity作为页面视图工具,struts2.3基础架构的搭建请查看 《架构搭建系统(一)Struts2基础架构搭建全过程》

velocity增加导入包后的项目lib清单


























web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>tech2ipo_blog</display-name>
  <!-- struts config -->
  	<filter>
	   <filter-name>ActionContextCleanUp</filter-name>
	   <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
	</filter>
	<filter>
	   <filter-name>struts2</filter-name>
	   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
	   <filter-name>ActionContextCleanUp</filter-name>
	   <url-pattern>*.action</url-pattern>
	</filter-mapping>
	<filter-mapping>
	   <filter-name>struts2</filter-name>
	   <url-pattern>*.action</url-pattern>
	</filter-mapping>
	
	<!-- velocity config -->
	<servlet>
		<servlet-name>velocity</servlet-name>
		<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
		<init-param>  
	    	<param-name>org.apache.velocity.toolbox</param-name>  
	    	<param-value>/WEB-INF/tools.xml</param-value>  
	  	</init-param>  
	   	<init-param>  
	    	<param-name>org.apache.velocity.properties</param-name>  
	    	<param-value>/WEB-INF/velocity.properties</param-value>  
	  	</init-param>
		<load-on-startup>10</load-on-startup>
	</servlet>
	
	<!-- Action Servlet Mapping -->
	<servlet-mapping>
		<servlet-name>velocity</servlet-name>
		<url-pattern>*.vm</url-pattern>
	</servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


struts.xml

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

<struts>
	<!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的输出 -->
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	
	<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
	<constant name="struts.devMode" value="true" />
	
	<!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
	<!-- <constant name="struts.action.extension" value="action" />  -->
	
	<constant name="struts.multipart.maxSize" value="20971521"/>
	
	<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
    	<constant name="struts.serve.static.browserCache" value="false"/>
    
    	<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	
    	<!-- 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false -->
	<!-- <constant name="struts.enable.DynamicMethodInvocation" value="true" /> -->
	
    <package name="default" namespace="/" extends="struts-default">
		 <action name="register" class="com.tech2ipo.action.UserAction" method="registerUser">
            <result name="success">/result.vm</result>
        </action>
    </package>
</struts>

WEB-INF\tools.xml

<?xml version="1.0" encoding="UTF-8"?>
<toolbox>
	<tool>
   		<key>tiles</key>
   		<scope>request</scope>
   		<class>org.apache.velocity.tools.struts.TilesTool</class>
 	</tool>
    <tool>
        <key>msg</key>
		<scope>request</scope>
        <class>org.apache.velocity.tools.struts.MessageTool</class>
    </tool>
    <tool>
        <key>errors</key>
		<scope>request</scope>
        <class>org.apache.velocity.tools.struts.ErrorsTool</class>
    </tool>
    <tool>
        <key>form</key>
		<scope>request</scope>
        <class>org.apache.velocity.tools.struts.FormTool</class>
    </tool>
	<tool>
        <key>date</key>
		<scope>application</scope>
        <class>org.apache.velocity.tools.generic.DateTool</class>
		<parameter name="format" value="yyyy-MM-dd"/>
    </tool>
	<tool>
   		<key>list</key>
   		<scope>application</scope>
   		<class>org.apache.velocity.tools.generic.ListTool</class>
 	</tool>
	<tool>
        <key>sort</key>
		<scope>application</scope>
        <class>org.apache.velocity.tools.generic.SortTool</class>
    </tool>
	<tool>
	   	<key>number</key>
	   	<scope>application</scope>
	   	<class>org.apache.velocity.tools.generic.NumberTool</class>
	</tool>
	<tool>
	 	<key>alternator</key>
	   	<scope>application</scope>
	   	<class>org.apache.velocity.tools.generic.AlternatorTool</class>
	   	<parameter name="auto-alternate" value="true"/>
	</tool>
	<tool>
	   	<key>esc</key>
	   	<scope>application</scope>
	   	<class>org.apache.velocity.tools.generic.EscapeTool</class>
	</tool>
</toolbox>

WEB-INF\velocity.properties

output.encoding=UTF-8
input.encoding=UTF-8
default.contentType=text/html; charset=UTF-8

先自己建一个User序列化实体类,UserAction类如下:
import com.opensymphony.xwork2.ActionSupport;
import com.test.user.client.User;

public class UserAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2131985071895417130L;
	
	private User user;
	
	public String execute() {
		return SUCCESS;
	}
	
	/**
	 * 用户注册
	 * @return
	 * @throws Exception
	 */
	public String registerUser() throws Exception {
		
        return SUCCESS;
    }

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
}

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
用户注册<br/>
<form action="register.action" method="post">
<input name="user.displayName" type="text" value=""/><br/>
<input name="user.userEmail" type="text" value=""/><br/>
<input name="user.userPass" type="password" value=""/><br/>
<input type="submit" value="注册"/>
</form>
</body>
</html>

result.vm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册结果页VM</title>
</head>
<body>
注册成功vm<br/>
<p>-------------------------------------------</p>
<p>${user.displayName}|${user.userEmail}|${user.userPass}</p>
<p>$date</p>
<a href="index.html">向注册页出发</a>
</body>
</html>


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页