Screenshot of the project structure as follows, the project is built by Maven Web project, the example is simple, no database connection operation, feature demo of the request address under the controller package of three classes, in this case the request address is:
Http://localhost:8080/spring-mvc-velocity-bootstrap/greet--Default Welcome
Http://localhost:8080/spring-mvc-velocity-bootstrap/greet/zhangsan--Welcome to someone, this is zhangsan, can be arbitrary
Http://localhost:8080/spring-mvc-velocity-bootstrap/hello--Default Hello
Http://localhost:8080/spring-mvc-velocity-bootstrap/hello-world--hello World
Http://localhost:8080/spring-mvc-velocity-bootstrap/hello-redirect-Redirect to Hello World request
Http://localhost:8080/spring-mvc-velocity-bootstrap/user/zhang/san.json--Request parameter is JSON format
Http://localhost:8080/spring-mvc-velocity-bootstrap/user/zhang/san.xml--Request parameter is XML format
The project is a simple springmvc+velocity+rest service with no database connection operations, below gives the request configuration under the controller package, and spring's XML file configuration, and velocity's template configuration, and web.xml loading, listening configuration.
The code for the request configuration of the three controller classes is:
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 you to the some values to controller using URL. * * * * @author Pmendelski * */@Controller public class Greetingscontroller {private final Logger Logger = Loggerfacto
Ry.getlogger (GetClass ()); @RequestMapping (value = "/greet/{name}", method = requestmethod.get) public String greetpath (@PathVariable String name, M
Odelmap 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 nam
E, 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 * * */@Controller public 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 ("Meth
OD helloWorld ");
return "Hello-world"; @RequestMapping (value = "/hello-redirect", method = requestmethod.get) public String helloredirect () {Logger.debu
G ("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 APIs. * @author Pmendelski * * */@Controller public 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.tra CE ("Responding service Re"Quest ");
User user = new user (name, surname);
return user; @RequestMapping (value = "/user/{name}/{surname}.xml", method = Requestmethod.get, produces = MEDIATYPE.APPLICATION_XM L_value) Public @ResponseBody User getuserxml (@PathVariable string name, @PathVariable string surname) {Logger.trace ("
Responding service request ");
User user = new user (name, surname);
return user;
}
}
Then the XML configuration for spring is as follows:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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/
Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schema/context http ://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> <!--Internationa Lization--> <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.we" B.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> &L T;/beans>
Then there are three velocity templates and the configuration of the properties files:
#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=true
velocimacro.permissions.allow.inline.to.replace.global=true
Velocimacro.permissions.allow.inline.local.scope=true
input.encoding=utf-8
output.encoding=utf-8
Resource.loader=webapp, class
class.resource.loader.description=velocity Classpath resource Loader
Class.resource.loader.class=org.apache.velocity.runtime.resource.loader.classpathresourceloader
Webapp.resource.loader.class=org.apache.velocity.tools.view.webappresourceloader
webapp.resource.loader.path=/web-inf/velocity/
Webapp.resource.loader.cache=false
Finally, the Web.xml configuration:
<?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.conte xtloaderlistener</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>
Other specific code implementation I have uploaded to the Http://download.csdn.net/detail/johnjobs/7139187,maven build completed, deployed to Tomcat and other containers, request the above request address, that is, you can see the effect demo, The following screenshot of the effect, the example is simpler: