Springmvc learning notes (10)-springmvc annotation development-product modification function
Springmvc learning notes (10)-springmvc annotation development-product modification function
This article takes product modification as an example to record springmvc annotation development, including the return values of mapper, service, controller, @ RequestMapping, and controller methods.
Requirement
Procedure:
1. Go to the product query list page. 2. click Modify to go to the product modification page. The item to be modified is displayed. Query the product information based on the product id (primary key). 3. On the product modification page, modify the product information, and click er.
Mapper:
Query item information by id update Items table data by id
You do not need to develop the code that is generated by reverse engineering.
Development service
Incom.iot.learnssm.firstssm.service.ItemsService
Add two interfaces
// Query product information by id /****
Title: findItemsById
*
Description:
* @ Param id: query the product id * @ return * @ throws Exception */ItemsCustom findItemsById (Integer id) throws Exception; // modify the product information /****
Title: updateItems
*
Description:
* @ Param id: Modify the item id * @ param itemsCustom the item information modified * @ throws Exception */void updateItems (Integer id, ItemsCustom itemsCustom) throws Exception;
Incom.iot.learnssm.firstssm.service.impl.ItemsServiceImpl
To additemsMapper
Attribute
@ Autowiredprivate ItemsMapper itemsMapper; public ItemsCustom findItemsById (Integer id) throws Exception {Items items = itemsMapper. selectByPrimaryKey (id); // process the product information in the middle //.... // return ItemsCustom itemsCustom = new ItemsCustom (); // copy the value of items to itemsCustom BeanUtils. copyProperties (items, itemsCustom); return itemsCustom;} public void updateItems (Integer id, ItemsCustom itemsCustom) throws Exception {// Add business validation, generally, key parameters are verified on the service interface. // If the id is null, an exception is thrown. // update the item information. Use updateByPrimaryKeyWithBLOBs to update all fields in the items table based on the id, fields of the large text type // updateByPrimaryKeyWithBLOBs must be transferred to the id itemsCustom. setId (id); itemsMapper. updateByPrimaryKeyWithBLOBs (itemsCustom );}
Develop controller
Method:
The item Information Modification page displays the item Information Modification and submission
// Use @ Controller to identify it as a Controller @ Controller // to manage URLs by category, you can define the root path here, the final access url is the root path + sub-path // For example: product list:/items/queryItems. action [email protected] ("/items") public class ItemsController {@ Autowired private ItemsService itemsService; // item query list @ RequestMapping ("/queryItems ") // ing the queryItems method and url. A method corresponds to a url. // It is generally recommended to write the url and method as public ModelAndView queryItems () throws Exception {// call the service to find the database and query the List of items
ItemsList = itemsService. findItemsList (null); // return ModelAndView modelAndView = new ModelAndView (); // It is equivalent to the setAttribute method of the request. You can use itemsList to retrieve data modelAndView on the jsp page. addObject ("itemsList", itemsList); // specify the path below the view. If you configure the jsp path prefix and Suffix in the view parser, change to items/itemsList // modelAndView. setViewName ("/WEB-INF/jsp/items/itemsList. jsp "); // The path configuration below can not specify the prefix and suffix modelAndView of the jsp path in the program. setViewName ("items/itemsList"); return modelAndView;} // The Product Information Modification page displays @ RequestMapping ("/editItems") // restrict http request methods, post and get [email protected] (value = "/editItems", method = {RequestMethod. POST, RequestMethod. GET}) public ModelAndView editItems () throws Exception {// call the service to query the item information ItemsCustom itemsCustom = itemsService Based on the item id. findItemsById (1); // return ModelAndView modelAndView = new ModelAndView (); // place the product information to model modelAndView. addObject ("itemsCustom", itemsCustom); // modelAndView on the item modification page. setViewName ("items/editItems"); return modelAndView;} // submit the message modification @ RequestMapping ("/attributes") public ModelAndView editItemsSubmit (HttpServletRequest request, Integer id, ItemsCustom itemsCustom) throws Exception {// call service to update product information. The itemsService method must be uploaded to the page. updateItems (id, itemsCustom); // return ModelAndView modelAndView = new ModelAndView (); // return a successful page modelAndView. setViewName ("success"); return modelAndView ;}}
@RequestMapping
Url ing
Define the url corresponding to the controller Method for processor ing.
Narrow request ing
// Use @ Controller to identify it as a Controller @ Controller // to manage URLs by category, you can define the root path here, the final access url is the root path + sub-path // For example: product list:/items/queryItems. action @ RequestMapping ("/items") public class ItemsController {
Restrict http request methods
For security reasons, restrict http links.
// The Product Information Modification page displays [email protected] ("/editItems") // restrict http request methods. you can post and get @ RequestMapping (value = "/editItems ", method = {RequestMethod. POST, RequestMethod. GET}) public ModelAndView editItems () throws Exception {
If the request is restricted to the post method, make a get request and change the annotation of the above Code@RequestMapping(value="/editItems",method={RequestMethod.POST})
Error, status code 405:
Controller Method Return Value
ModelAndView
At the end of the method, define ModelAndView and set the model and view respectively.
Returns string
If the controller method returns string
1. indicates the name of the returned logical view.
Real view (jsp path) = prefix + logical view name + suffix
@ RequestMapping (value = "/editItems", method = {RequestMethod. POST, RequestMethod. GET}) in [email protected], specify the request input parameter name and parameter to bind. // Use the required attribute to specify whether the parameter must be passed in. // use defaultValue to set the default value. If the id parameter is not passed in, bind the default value to the parameter. // Public String editItems (Model model, @ RequestParam (value = "id", required = true) Integer items_id) throws Exception {public String editItems (Model model) throws Exception {// call service to query the item information ItemsCustom itemsCustom = itemsService Based on the item id. findItemsById (1); // upload the model data to the page using the model in the parameter. // It is equivalent to modelAndView. addObject method model. addAttribute ("itemsCustom", itemsCustom); return "items/editItems ";}
2. redirect redirection
After the item is submitted, the system redirects to the item query list.
Redirect redirection features: the url in the browser's address bar changes. Modifying submitted request data cannot be passed to the redirection address. Because the request is re-submitted after redirection (the request cannot be shared)
// Redirect to the product query list // return "redirect: queryItems. action ";
3. forward pages
Forward pages through forward. The url in the browser address bar remains unchanged and requests can be shared.
// Return "forward: queryItems. action ";
Return void
You can define request and response in the controller method parameters, and use request or response to specify the response result:
1. Use the request to redirect to the page, as shown below:
Request. getRequestDispatcher ("Page path"). forward (request, response );
2. You can also redirect through the response page:
response.sendRedirect("url")
3. You can also specify the response result through response. For example, the response json data is as follows:
Response. setCharacterEncoding ("UTF-8"); response. setContentType ("application/json; charset = UTF-8"); response. getWriter (). write ("json string ");