Spring MVC整合Velocity的例子

本文分享了一个简单的SpringMVC与Velocity模板引擎整合的例子。通过配置web.xml和mvc-servlet.xml,实现页面显示数据,包括对象属性展示、条件判断、循环语句等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     上午简单的学了下springmvc和velocity的整合,跟大家分享下,项目结构见附件。

 1、Person.java是一个简单的JavaBean代码如下:

package com.test.bean;

import java.util.Date;

public class Person {

	private int id;
	
	private String name;
	
	private String password;
	
	private int age;
	
	private Date birthday;
	

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	} 
}

 2、web.xml配置如下

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
  <servlet-name>mvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
   <servlet-name>mvc</servlet-name>
   <url-pattern>*.html</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>

 3、mvc-servlet.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
	</bean>
	<bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer" p:resourceLoaderPath="/WEB-INF/velocity">
	   <property name="velocityProperties">
	      <props>
	        <prop key="directive.foreach.counter.name">loopCounter</prop>
        	<prop key="directive.foreach.counter.initial.value">0</prop>
        	<prop key="input.encoding">UTF-8</prop>
        	<prop key="output.encoding">UTF-8</prop>
	      </props>
	    </property>
	</bean>
	<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"
	 p:suffix=".html" p:contentType="text/html; charset=UTF-8" p:exposeRequestAttributes="true" p:exposeSessionAttributes="true" p:dateToolAttribute="dateTool" p:numberToolAttribute="numberTool"/>  
	<bean name="/index.html" class="com.test.controller.IndexController"/>
</beans>

 4、模板文件index.html

 

<!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>Insert title here</title>
</head>
<body>
显示对象的属性<br>
$person.id<br>
$person.name<br>
$person.password<br>
$person.age<br>
if语句<br>
#if($person.id <2)
id大于2
#else
id小于2
#end
#if($person.password=="123456")
密码等于123456
#end
<hr>
foreach语句<br>
#foreach($p in $persons)
	$p.id<br>
   $p.name<br>
   $!p.email<br>
   $dateTool.format("yyyy-MM-dd HH:mm:ss", $p.birthday)<br>
#end
<hr>
request范围数据<br>
$requestData<br>
session范围数据<br>
$sessionData<br>
</body>
</html>

 5、控制器

package com.test.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.test.bean.Person;

public class IndexController implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView("main");
		
		Person p1 = new Person();
		p1.setId(1);
		p1.setName("胡楠");
		p1.setPassword("123456");
		p1.setAge(24);
		p1.setBirthday(new Date());
		
		List<Person> list = new ArrayList<Person>();
		for(int i=0;i<10;i++){
			Person p = new Person();
			p.setId(1000+i);
			p.setName("胡楠"+i);
			p.setPassword("123456"+i);
			p.setAge(24+i);
			p.setBirthday(new Date());
			list.add(p);
		}
		//集合
		mav.addObject("persons", list);
		//对象
		mav.addObject("person",p1);
		//request范围数据
		request.setAttribute("requestData", "hunan");
		//session范围数据
		request.getSession().setAttribute("sessionData", "123456");
		return mav;
	}

}

  启动tomcat,输入http://xxx.renren.com:8080/index.html。

 

内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文不仅提供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值