springmvc+velocity+ Rest Services(xml,json)实例
来源:互联网 发布:淘宝如何买爱情动作片 编辑:程序博客网 时间:2024/06/06 07:37
项目结构截图如下,该项目由maven构建的web项目,实例简单,无数据库连接操作,功能演示的请求地址分别在controller包下的三个类中,本例中的请求地址为:
http://localhost:8080/spring-mvc-velocity-bootstrap/greet --默认欢迎
http://localhost:8080/spring-mvc-velocity-bootstrap/greet/zhangsan --欢迎某人,这里是zhangsan,可任意
http://localhost:8080/spring-mvc-velocity-bootstrap/hello --默认hello
http://localhost:8080/spring-mvc-velocity-bootstrap/hello-world --hello world
http://localhost:8080/spring-mvc-velocity-bootstrap/hello-redirect --重定向到hello world请求
http://localhost:8080/spring-mvc-velocity-bootstrap/user/zhang/san.json --请求参数为json格式
http://localhost:8080/spring-mvc-velocity-bootstrap/user/zhang/san.xml --请求参数为xml格式
该项目为简单的springmvc+velocity+rest service,且无数据库连接操作,下面给出controller包下的请求配置,和spring的xml文件配置,和velocity的模板配置,和web.xml的加载,监听配置。
三个controller类的请求配置的代码依次为:
package net.exacode.bootstrap.web.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;/** * Presents how to pass some values to controller using URL. * * @author pmendelski * */@Controllerpublic class GreetingsController {private final Logger logger = LoggerFactory.getLogger(getClass());@RequestMapping(value = "/greet/{name}", method = RequestMethod.GET)public String greetPath(@PathVariable String name, ModelMap model) {logger.debug("Method greetPath");model.addAttribute("name", name);return "greetings";}@RequestMapping(value = "/greet", method = RequestMethod.GET)public String greetRequest(@RequestParam(required = false, defaultValue = "John Doe") String name,ModelMap model) {logger.debug("Method greetRequest");model.addAttribute("name", name);return "greetings";}}
package net.exacode.bootstrap.web.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/** * Simple hello world controller. * Presents basic usage of SpringMVC and Velocity. * @author pmendelski * */@Controllerpublic class HelloWorldController {private final Logger logger = LoggerFactory.getLogger(getClass());@RequestMapping(value = "/hello", method = RequestMethod.GET)public String hello() {logger.debug("Method hello");return "hello";}@RequestMapping(value = "/hello-world", method = RequestMethod.GET)public String helloWorld() {logger.debug("Method helloWorld");return "hello-world";}@RequestMapping(value = "/hello-redirect", method = RequestMethod.GET)public String helloRedirect() {logger.debug("Method helloRedirect");return "redirect:/hello-world";}}
package net.exacode.bootstrap.web.controller;import net.exacode.bootstrap.web.model.User;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.http.MediaType;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;/** * Service hello world controller. * Presents basic usage of SpringMVC and REST service API. * @author pmendelski * */@Controllerpublic class UserServiceController {private final Logger logger = LoggerFactory.getLogger(getClass());@RequestMapping(value = "/user/{name}/{surname}.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)public @ResponseBody User getUserJson(@PathVariable String name, @PathVariable String surname) {logger.trace("Responding service request");User user = new User(name, surname);return user;}@RequestMapping(value = "/user/{name}/{surname}.xml", method = RequestMethod.GET, produces = MediaType.APPLICATION_XML_VALUE)public @ResponseBody User getUserXml(@PathVariable String name, @PathVariable String surname) {logger.trace("Responding service request");User user = new User(name, surname);return user;}}
然后是spring的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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:sec="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><mvc:annotation-driven /><mvc:default-servlet-handler/><mvc:resources mapping="/resources/**" location="/resources/" /><context:component-scan base-package="net.exacode.bootstrap.web" /><bean id="velocityConfig"class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"><property name="configLocation"><value>/WEB-INF/velocity/velocity.properties</value></property></bean><bean id="viewResolver"class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver"><property name="cache" value="false" /><property name="layoutUrl" value="/layout/main.vm" /><property name="prefix" value="/templates/" /><property name="suffix" value=".vm" /><property name="exposeSpringMacroHelpers" value="true" /><property name="contentType" value="text/html;charset=UTF-8" /><property name="viewClass"value="org.springframework.web.servlet.view.velocity.VelocityLayoutView" /></bean><!-- Internationalization --><bean id="messageSource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><property name="basename" value="classpath:i18n/messages" /><property name="defaultEncoding" value="UTF-8" /></bean><bean id="localeChangeInterceptor"class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"><property name="paramName" value="lang" /></bean><bean id="localeResolver"class="org.springframework.web.servlet.i18n.CookieLocaleResolver"><property name="defaultLocale" value="en" /></bean><bean id="handlerMapping"class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"><property name="interceptors"><ref bean="localeChangeInterceptor" /></property></bean></beans>
然后是三个velocity的模板和属性文件的配置:
#define($content)<p>#springMessage("Hello"): $name</p><p>#springMessage("Greetings")</p>#end
#define($content)#springMessage("Hello")#end
#set($page_title="#springMessage('HelloWorld')")#define($content)#springMessage("HelloWorld")#end
velocimacro.permissions.allow.inline=truevelocimacro.permissions.allow.inline.to.replace.global=truevelocimacro.permissions.allow.inline.local.scope=trueinput.encoding=UTF-8output.encoding=UTF-8resource.loader=webapp, classclass.resource.loader.description=Velocity Classpath Resource Loaderclass.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoaderwebapp.resource.loader.path=/WEB-INF/velocity/webapp.resource.loader.cache=false
最后是web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- The definition of the Root Spring Container shared by all Servlets and Filters --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><!-- Creates the Spring Container shared by all Servlets and Filters --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener><servlet><servlet-name>springDispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springDispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
其他具体代码实现我已上传到http://download.csdn.net/detail/johnjobs/7139187,maven构建完成之后,部署到tomcat等容器下,请求上面给到的请求地址,即可以看到效果演示,以下为效果截图,例子较为简单:
- springmvc+velocity+ Rest Services(xml,json)实例
- springmvc+velocity+ Rest Services(xml,json)实例
- Springmvc RestTemplate 使用 && json xml rest 配置
- Parsing REST Services JSON Responses (C#)
- Itop REST/JSON services 验证登陆方法
- springmvc rest服务deom实例
- JSON+REST vs. XML+SOAP
- springmvc json xml
- Consuming XML and JSON web services (MyMoviesWithHttpClient)
- spring+cxf+maven+rest风格+json/xml
- SpringMVC整合JSON、XML视图
- SpringMVC整合JSON、XML视图
- SpringMVC整合JSON、XML视图
- SpringMVC整合JSON、XML视图
- SpringMVC返回XML和JSON
- springmvc+spring jdbc+velocity入门教程及其代码实例
- 小白零xml搭建springmvc+Velocity+mybatis之笔记
- springMVC(1)------springMVC之XML实例
- Android控件RatingBar的使用
- Jsp /js乱码问题 文件,页面,浏览器编解码
- 有技术经验的开发人员转型iPhone开发的培训大纲
- 人类创造未来的思想先锋:这些 TED 演示深深震撼着我们
- STL 迭代器
- springmvc+velocity+ Rest Services(xml,json)实例
- Maven web工程打包自动添加时间戳后缀
- 建站仿站扒站必备工具之ScrapBook火狐插件推
- ftpget 到开发板
- 观察者与执行者
- JAVA的try,catch 和 finally
- error---cin
- 液晶中Duty和COM的含义
- STL 分配器