SpringMVC (RESTful) and springmvcrestful

Source: Internet
Author: User

SpringMVC (RESTful) and springmvcrestful

I. Core Principles

1. Used to send a request to the server:/home.htm

2. The request is intercepted by DispatchServlet

3. DispatchServlet checks whether the url has a corresponding Controller through HandleMapping. If yes, call the Controller

4. The Controller starts to execute the business logic.

5. After the Controller is executed, if a string is returned, ViewResolver converts the string to the corresponding view object;

If the ModelAndView object is returned, the object itself contains the view object information.

6. DispatchServlet outputs data from the view object to the server.

7. The server outputs data to the client.

Ii. REST style

1. Query:
Traditional:/user_query? Id = 123
Rest:/user/123
2. Delete:
Traditional:/user_delete? Id = 123
Rest:/user/123/delete
3. Update:
Traditional:/user_update? Id = 123
Rest:/user/123/update
4. List:
Traditional:/user_list
Rest:/user/users


3. add, delete, modify, and query

1. code structure



2. Configure DispatchServlet in 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" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5"> <! -- User needs to correspond to the user-servlet.xml name of the configuration file --> <servlet-name> user </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> user </ servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <! -- Configure the filter and set the encoding to UTF-8 --> <filter-name> CharacterFilter </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> </filter> <filter-mapping> <filter-name> CharacterFilter </filter-name> <url-pattern>/* </url-pattern> </filter- mapping> </web-app>


3. user-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: context = "http://www.springframework.org/schema/context" xmlns: mvc = "http://www.springframework.org/schema/mvc" xsi: schemaLocation = "http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http: // www .Springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd "> <! -- Spring scan package --> <context: component-scan base-package = "com. zdp"> </context: component-scan> <! -- Use spring annotation --> <mvc: annotation-driven/> <! -- Set the returned url prefix and suffix --> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" viewClass "value =" org. springframework. web. servlet. view. jstlView "/> <property name =" prefix "value ="/WEB-INF/jsp/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean> <! -- Specify the static resource location --> <mvc: resources location = "/WEB-INF/resources/" mapping = "/resources/**"/> </beans>

4. User. java

/*** Entity class */public class User {private String username; private String password; public User () {} public User (String username, String password) {this. username = username; this. password = password;} @ NotEmpty (message = "username cannot be blank") public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} @ Size (min = 1, max = 10, message = "the password length should be between 1 and 10") public String getPassword () {return password ;} public void setPassword (String password) {this. password = password ;}}

5. UserController. java

@ Controller @ RequestMapping ("/user") public class UserController {// simulate database private map using Map <String, User> userMap = new HashMap <String, User> (); public UserController () {userMap. put ("zhangsan", new User ("zhangsan", "123"); userMap. put ("lishimin", new User ("lishimin", "456");} // get the User list // access method: http: // localhost/springmvc_user/user/users @ RequestMapping (value = "/users", method = RequestMethod. GET) public String list (Model model) {model. addAttribute ("users", userMap); return "/user/list";} // jump to the add user page (get request) // access method: http: // localhost/springmvc_user/user/add @ RequestMapping (value = "/add", method = RequestMethod. GET) public String add (@ ModelAttribute ("user") User user) {return "user/add";} // add user processing method (post request) // Note: The BindingResult must be after the User, and no other parameter @ RequestMapping (value = "/add", method = RequestMethod. POST) public String add (@ Validated User user, BindingResult br) throws IOException {// @ Validated: Checks User data if (br. hasErrors () {return "user/add"; // if an error occurs, go directly to the add user page} userMap. put (user. getUsername (), user); return "redirect:/user/users"; // redirect to the user list page} // view user Information // access method: http: // localhost/springmvc_user/user/zhangsan @ RequestMapping (value = "/{username}", method = RequestMethod. GET) public String show (@ PathVariable String username, Model model) {// @ PathVariable: the value in the path is used as the parameter model. addAttribute (userMap. get (username); return "user/show";} // jump to the modify user information page // access method: http: // localhost/springmvc_user/zhangsan/update @ RequestMapping (value = "/{username}/update", method = RequestMethod. GET) public String update (@ PathVariable String username, Model model) {// @ PathVariable: the value in the path is used as the parameter model. addAttribute (userMap. get (username); // equivalent: model. addAttribute ("user", userMap. get (username); return "user/update";} // modify the User processing method (post request) // Note: The BindingResult must be after the user, there cannot be other parameters @ RequestMapping (value = "/{username}/update", method = RequestMethod. POST) public String update (@ PathVariable String username, @ Validated User user, BindingResult br) {if (br. hasErrors () {return "user/update"; // if an error exists, go directly to the update user modification page} userMap. remove (username); userMap. put (user. getUsername (), user); return "redirect:/user/users" ;}// delete user information // access method: http: // localhost/springmvc_user/zhangsan/delete @ RequestMapping (value = "/{username}/delete", method = RequestMethod. GET) public String delete (@ PathVariable String username) {userMap. remove (username); return "redirect:/user/users ";}}

6. jsp pages

① List. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" c "uri =" http://java.sun.com/jsp/jstl/core "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

② Add. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" sf "uri =" http://www.springframework.org/tags/form "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> ③ Show. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" sf "uri =" http://www.springframework.org/tags/form "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> ④ Update. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" sf "uri =" http://www.springframework.org/tags/form "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 







Java uses spring mvc + hibernate to add, delete, modify, and query Databases

1) Configure dataSource and SessionFactory of Hibernate in applicationContext
2) The Dao interface is an implementation class. If you use the Hibernate operation template of Spring (HibernateTemplate), you can inherit the HibernateDaoSupport and implement class annotation to @ Repository
3) write the Action and annotate it as @ Controller. In the Action, @ Autowired must be used to inject Dao instances.
4) Configure applicationContext. xml with the view processor of Spring MVC
5) write the page and submit the request. Done!

Add, delete, modify, and query in spring mvc

Www.blogjava.net/..w.admin sent your email.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.