SSH配置(四)-sitemesh-velocity

紧接SSH配置(三)之后。

一、sitemesh配置

sitemesh包:sitemesh-2.4.2.jar

1、在WebContent->WEB-INF->lib下导入sitemesh所需jar包

路径:\sitemesh\


2、在src下添加action类文件和在WEB-INF下添加web页面

①在src下添加action类文件


UserAction.java类代码:

package com.jjh.test.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.jjh.ssh.service.UserService;
import com.jjh.ssh.service.impl.UserServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/user")
@Result(name = "userlist", location = "userlist.vm")

public class UserAction extends ActionSupport {
	
	@Autowired
	UserService userService;
	
	private List userList;
	
	/**
	 * 获取用户列表
	 * @return
	 * @throws Exception
	 */
	public String listUsers() throws Exception{
		System.out.println("-------listuser action begin-------");
		
		try {
			this.userList = userService.listUsers();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("-------listuser action end-------");
		
		return "userlist";
	}

	public List getUserList() {
		return userList;
	}

	public void setUserList(List userList) {
		this.userList = userList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
	
	
}

②在WEB-INF下添加view->user->userlist.vm


userlist.vm代码:

fsdfsdfsdfsdfsdfsdfsfsfs

#foreach($item in $userList)
	<div>$item.name</div>
	<div>$item.chname</div>
	<div>$item.password</div>
#end
3、在src目录下添加decorator.xml 和 decorator文件夹

①decorator下设置默认装饰页面default.vm

<!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" />
<meta http-equiv="Cache-Control" content="no-store"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="X-UA-Compatible" content="IE=9">
<title>ssh项目</title>
</head>
<body>
<div>
	$body
</div>
</body>
</html>
        

②decorator.xml文件代码

<?xml version="1.0" encoding="ISO-8859-1"?>

<decorators defaultdir="/decorators">
    <!-- Any urls that are excluded will never be decorated by Sitemesh -->
    <excludes>
        <pattern>/exclude.jsp</pattern>
        <pattern>/exclude/*</pattern>
    </excludes>
     
    <decorator name="default" page="default.vm">
        <pattern>/user/user!listUsers.do</pattern>
    </decorator>
</decorators>


二、配置velocity

velocity包:velocity-1.6.2.jar、velocity-tools-2.0.jar

1、在WebContent->WEB-INF->lib下导入velocity所需jar包

路径:\velocity-tools-2.0\lib\


2、在src目录下添加velocity.properties


velocity.properties代码:

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# ----------------------------------------------------------------------------
# T E M P L A T E  E N C O D I N G
# ----------------------------------------------------------------------------
input.encoding=UTF-8
output.encoding=UTF-8

# ----------------------------------------------------------------------------
# VELOCIMACRO PROPERTIES
# ----------------------------------------------------------------------------
# global : name of default global library.  It is expected to be in the regular
# template path.  You may remove it (either the file or this property) if
# you wish with no harm.
# ----------------------------------------------------------------------------
#velocimacro.library.autoreload = false
#velocimacro.library = /WEB-INF/VM_global_library.vm

说明:目前只配置 输入输出页面编码格式


三、配置web.xml

直接贴完整的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>Test_Web</display-name>
  
  	<!-- 上下文参数 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/spring-*.xml</param-value>
	</context-param>
	 <!-- 上下文加载监听 --> 
	<listener>
	 	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener> 
	 
 	<!-- 编码过滤器 -->
	<filter>
	<filter-name>encodingFilter</filter-name>
	 	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
  	
	<!-- 类似ActionContextCleanUp过滤器 -->
	<filter>
		<filter-name>struts-prepare</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
	</filter>
	<!-- sitemesh过滤器 -->
	<filter>
		<filter-name>sitemesh</filter-name>
		<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
	</filter>
 	<!-- struts2过滤器 -->
	<filter>
		<filter-name>struts-execute</filter-name>
	  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class>
  	</filter>
  	
  	<!-- 过滤器链 -->
  	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
	  	<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>struts-prepare</filter-name>
	 	<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>sitemesh</filter-name>
	 	<url-pattern>/*</url-pattern>
	</filter-mapping>
 	<filter-mapping>
		<filter-name>struts-execute</filter-name>
	  	<url-pattern>/*</url-pattern>
	</filter-mapping>
	 
	 <!-- velocity 模版 -->
	<servlet>
		<servlet-name>sitemesh-velocity</servlet-name>
		<servlet-class>com.opensymphony.module.sitemesh.velocity.VelocityDecoratorServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	 	<servlet-name>sitemesh-velocity</servlet-name>
	 	<url-pattern>*.vm</url-pattern>	
	</servlet-mapping>
	 
	 	<!-- 
	<resource-ref> 
		<description>DB Connection</description>  
		<res-ref-name>jdbc/ssh</res-ref-name>  
		<res-type>javax.sql.DataSource</res-type>  
		<res-auth>Container</res-auth>  
	</resource-ref>
	 -->
	 
	<welcome-file-list>
		  <welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


四、验证

浏览器中输入:http://localhost:8080/SSHFW/user/user!listUsers.do

显示如图:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值