I. Advanced parameter binding1, the front end of the parameter binding array/collection
1) Array Direct receive
@RequestMapping (value = "/arraytest.action") public void Arraytest (integer[] IDs) { System.out.println ( Arrays.tostring (IDs));}
2) array reception in Pojo
@RequestMapping (value = "/arraytest.action") public void Arraytest (Queryvo vo) { System.out.println ( Arrays.tostring (Vo.getids ()));}
3) Collection direct receive (error, cannot receive directly)
@RequestMapping (value = "/arraytest.action") public void Arraytest (list<integer> IDs) { System.out.println ( IDS);}
4) Collection reception in Pojo
@RequestMapping (value = "/arraytest.action") public void Arraytest (Queryvo vo) { System.out.println (Vo.getids ());}
2. Save all the parameters from the front end to the collection
JSP page Styles
<form action= "${pagecontext.request.contextpath}/arraytest.action" method= "POST" > Product list: <table width= "100% "Border=1><tr><td> selection </td><td> Product name </td><td> commodity price </td><td> production date </td><td> Product Description </td><td> operation </td></tr><c:foreach items= "${itemlist}" var= " Item "varstatus=" s "><tr><td><input type=" checkbox "Name=" IDs "value=" ${item.id} "></td> <td><input type= "text" name= "Itemslist[${s.index}].name" value= "${item.name}" ></td><td> <input type= "text" name= "Itemslist[${s.index}].price" value= "${item.price}" ></TD><TD><FMT: FormatDate value= "${item.createtime}" pattern= "Yyyy-mm-dd HH:mm:ss"/></td><td>${item.detail}</ Td><td><a href= "${pagecontext.request.contextpath}/itemedit.action?id=${item.id}" > Modify </a> </td></tr></c:foreach></table><input type= "Submit"/></form>
Queryvo.java
public class Queryvo { list<item> itemslist; Public list<item> getitemslist () { return itemslist; } public void Setitemslist (list<item> itemslist) { this.itemslist = itemslist; }}
Test class
@RequestMapping (value = "/arraytest.action") public void Arraytest (Queryvo vo) { System.out.println ( Vo.getitemslist ());}
two [email protected]
1. URL route map
@RequestMapping (value = "/xxx.action")
2, on the class to add
Simplified request path/itemlist ==>/item/itemlist
@RequestMapping ("/item")
public class Itemscontroller {
3. Method limit
Limited GET Method:@RequestMapping (method = Requestmethod. GET)
Limited POST Method:@RequestMapping (method = Requestmethod. POST)
Both get and POST can:@RequestMapping (method = {Requestmethod. GET, Requestmethod. POST})
4. Multiple URLs mapped to the same method
@RequestMapping (value = "{/itemlist.action,/item123.action}")
three. Controller method return value1. Return to Modelandview
The Controller method defines the Modelandview object and returns it, adding model data to the object, specifying the view. (typically used in exception handlers)
Configure the requested url@requestmapping (value = "/itemlist.action") public Modelandview queryitemlist () { //Create the product data that the page needs to display list<item> items = itemservice.selectitemslist (); Modelandview Modelandview = new Modelandview (); Put in the Request field modelandview.addobject ("ItemList", items); Modelandview.setviewname ("/web-inf/jsp/itemlist.jsp"); Modelandview.setviewname ("ItemList"); return Modelandview;}
2. Return string
Pros: Decoupling, return to view path, model with data, officially recommended this way. Decoupling, data, view, Detach, MVC.
Configure the requested url@requestmapping (value = "/itemlist.action") public String Queryitemlist (model model) { //create the product data that the page needs to display list<item> items = itemservice.selectitemslist (); Put in the Request field Model.addattribute ("ItemList", items); return "ItemList"; Directly returns a view file (JSP) //return "redirect:/itemedit.action?itemid=1"; Redirect to a different URL return "forward:/itemedit.action?itemid=1"; Forward to another URL}
3. Return void (commonly used in Ajax)
/Configure the requested url@requestmapping (value = "/itemlist.action") public void Queryitemlist (HttpServletRequest request, HttpServletResponse response, HttpSession session, model model) throws Servletexception, IOException { // Create a page to display the product data list<item> items = itemservice.selectitemslist (); Request.setattribute ("ItemList", items); Request forward Request.getrequestdispatcher ("/web-inf/jsp/itemlist.jsp"). Forward (request,response);}
Four. Exception handler
Springmvc exception information is processed by the exception handler during the processing of the request, and the custom exception handler can implement a system exception handling logic.
There are two kinds of exceptions in the system: expected exception and run-time exception runtimeexception, the former obtains exception information by catching exception, the latter mainly by standardizing code development, testing through means to reduce the occurrence of run-time exception.
The DAO, service, controller of the system is thrown up through throws exception, and finally by the SPRINGMVC Front controller to the exception processor for exception processing, such as:
Custom Exception Handlers
1) Customizing an exception Myexception.java
public class MyException extends Exception { private String msg; Public myexception (String msg) { this.msg = msg; } Public MyException () { } public String getmsg () { return msg; } public void Setmsg (String msg) { this.msg = msg; }}
2) manually throw this exception in the program
Configure the requested url@requestmapping (value = "/itemlist.action") public void Queryitemlist (HttpServletRequest request, HttpServletResponse response, HttpSession session, model model) throws Exception { if (1==1) { throw new Myexcepti On ("La La La"); Create a page to display the product data list<item> items = itemservice.selectitemslist (); Request.setattribute ("ItemList", items); Request forward Request.getrequestdispatcher ("/web-inf/jsp/itemlist.jsp"). Forward (request,response);}
3) Configuring the global exception handler in Applicationcontext.xml
<!--***************** Configure global exception processor *****************--><bean class= " Cn.x5456.exception.CustomExceptionResolver "/>
4) Write exception handler
Import Org.springframework.web.servlet.HandlerExceptionResolver; Do not lead the wrong package public class Customexceptionresolver implements Handlerexceptionresolver { @Override public Modelandview resolveexception (httpservletrequest httpservletrequest, httpservletresponse HttpServletResponse, Object O,//Where the exception occurred serivce Layer method package name + class name + method name (formal parameter) string Exception e) { Modelandview Mav = new Modelandview (); String msg = "Unknown exception"; if (e instanceof myexception) { myexception Myex = (myexception) e; msg = Myex.getmsg (); } Mav.addobject ("msg", msg); Mav.setviewname ("error"); return MAV;} }
springmvc--