RESTful architecture is an Internet software architecture that is currently popular. Its structure is clear, conforms to the standard, easy to understand, expands conveniently, therefore is getting more and more website adoption. The restful schema makes the URL specification, what does a restful URL look like? The URL we normally request is something like this:
http://...../xxx.action?id=001&type=aaa
And what does restful's URL style look like? Generally it resembles the following:
http://..../xxx/001
So rest has an obvious feature: making URLs simple and uploading parameters to the server via URLs. SPRINGMVC is also a support for this restful style URL, we define a controller to test:
//查询商品信息,输出json,使用RESTful@RequestMapping("/itemsView/{id}")publicitemsView(@PathVariablethrows Exception { ItemsCustom itemsCustom = itemsService.findItemsById(id); return itemsCustom;}
@ResponseBody is the annotation used to turn Itemscustom into JSON, while @pathvariable annotations are related to rest, @RequestMapping (value= "/Itemsview/{id}") {ID } represents a placeholder, the value passed in will be passed to the parameter that is @pathvariable, and if the parameter is the same as a variable in the placeholder, it can be specified in the annotation, otherwise the variable in the note (that is, the ID) is specified in the annotation. In this case, the parameters can be passed to the formal parameter via a URL.
But it's not going to work, and it's also configured for rest in the front-end controller, as follows:
<!--configuration SPRINGMVC Front Controller dispatcherservlet,rest configuration--<servlet> <servlet-name>Springmvc_rest</servlet-name> <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>Contextconfiglocation</param-name> <param-value>Classpath:spring/springmvc.xml</param-value> </init-param></servlet><servlet-mapping> <servlet-name>Springmvc_rest</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
Interception is all the URL (/), this configuration with the previously configured front-end controller is no conflict, can coexist. After this configuration, you can enter HTTP://LOCALHOST:8080/SPRINGMVC_STUDY/ITEMSVIEW/1 in the browser to test the data returned to the browser, and you can see that a string of JSON data is returned.
However, there is a problem, using the above configuration will block all URLs, then the static resources will also intercept, so Dispatcherservlet will also parse the static resources, but this will be an error, so we have to set up not to let it parse static resources. Such as:
<!-- 静态资源解析,包括js,css,img... --><mvc:resources location="/js/" mapping="/js/**"></mvc:resources><mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
If there are other static resources, but also to set up, so that the static resources will not be parsed, access to static resources when the direct access can be.
Related reading: http://blog.csdn.net/column/details/spring-mvc.html
Learning Note Source: Https://github.com/eson15/SpringMVC_Study
-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15
"Springmvc Learning 10" SPRINGMVC support for Restfull