SPRINGMVC return to MVC nature, simple restful functions, without any base class, should be the best use of the traditional request-response framework.
Tips1. Tragic failure of the business
Spring MVC is the hardest thing to hit newcomers, you have to make sure that Spring-mvc.xml's context:component-scan only scans the controller, and The controller is not included in the Applicationcontext.xml. Otherwise, the business you define in Applicationcontext.xml will fail. Here's how:
Spring-mvc.xml:
base-package=use-default-filters="false">type=expression=" Org.springframework.stereotype.Controller "/></context:component-scan>
Applicationcontext.xml:
Base-package= "org.springside.examples.quickstart">type=expression=" Org.springframework.stereotype.Controller "/></context:component-scan>
In addition, what is defined in the Spring-mvc.xml, is not visible in the Applicationcontext*.xml, the thing that wants to share is best put on Applicationcontext.xml yonder.
And some of the beanpostproccesor in Applicationcontext*.xml do not work on spring-mvc.xml defined/scanned beans, If necessary, redefine it in Spring-mvc.xml, like Shiro's AOP checksum permissions.
2.STRUTS2-Preparable interface--The form contains only the object realm part attributes
Struts2 has a very useful preparable two-time binding function: When the form commits, it binds an ID, uses this ID to find the object from the database, and then binds the other attributes in the form to the object. It is useful for those forms where the number of input boxes is less than the actual number of properties of the business object.
In fact, Spring MVC also has the same ability, see QuickStart in the Useradmincontroller.
The following functions are labeled with @modelattribute first. SPRINGMVC executes the function and saves the return value as model Attribute "user" before executing any actual processing functions.
@ModelAttribute("User")public user getuser (@ Requestparam (value = "id" required = false) long Id) {if (id != Span class= "KC" >null) {return accountservicegetuser (id} return null;
In the Save function, the parameters of the form processing function are labeled @modelattribute. SPRINGMVC will remove the previous object by the name "user" before the true binding is performed.
@RequestMapping (value = "Update/{userid}" method = requestmethod. Post) public string update ( Span class= "nd" > @Valid @ModelAttribute ( "user" user user) {accountservice updateuser (user "Redirect:/admin/user" ;
Note 1, there is a little pit daddy here, this getuser () will be executed in front of all of the controller's functions, so it is necessary to determine whether the Requestparam contains the id attribute of the judgment, or you will update () method to be independent of a controller.
Note 2, Modelattribute if the name "user" is already in use, avoid the name in the parameters of the non-update () function.
Alternatively, you can choose not to use this function, but create a dto of the form yourself, and then bind the attribute to the domain object with dozer or by hand.
3.STRUTS2 type of Flashattribute
To prevent users from refreshing duplicate commits, the save operation typically redirect to another page, with a hint of the success of the point operation. Because it is the redirect,request in the attribute will not pass the past, if placed in the session, you need to clean up after the display, or the following each page with this information is not right. Spring provides this capability in 3.1.
public string save (@ Modelattribute ( "group" group Groupredirectattributes redirectattributes) Span class= "o" >{accountmanager. Savegroup (group. ( "message" "Modify permission Group succeeded" return "redirect:/account/group/" ;
Bindings for 4.checkbox/radiobuttons
In an ORM-based application, it is a headache to bind sub-objects to the page and how to re-bind the contents of the checkbox back to the parent object when the form is submitted.
In the showcase example, role in the User-role combination is an object rather than a simple enumeration (for a simple enumeration, nothing is done, directly with the taglib of the checkboxes.
Path=items=itemlabel=itemvalue=/>
Note that if you use BOOTSTRAP,SPRINGMVC's own checkboxes label, see the Twitter bootstrap section.
and complex objects when the object is not so good color, see Showcase in the Usercontroler first you need to set not to automatically bind checkbox results to the object
@InitBinderinitbinder(binder{binder). Setdisallowedfields("rolelist");}
Then inject one more rolelist into the input parameters and handle it yourself:
@Requestmapping(Value= "Save/{Userid}")PublicStringUpdate(@Valid@Modelattribute("User)UserUser,@Requestparam(Value= "Rolelist ")List<Long>Checkedrolelist){User.Getrolelist().Clear();For(Longroleid : checkedrolelist {role role = new role ( span class= "n" >roleid); user. Getrolelist (). Add (role} accountservice. Saveuser (userredirect:/account/user ";} 5. Output JSONP required for cross-domain Ajax
Online say what expands jsonview what is too complicated, oneself take Jackson to generate a JSONP string return just fine. See the Ajax chapter for more JSONP information.
The combination of 6.Spring MVC and Hibernate validator
See Validation section, typically using jquery Validation plugin for client authentication. To prevent malicious users from attacking, you can add spring MVC and Hibernate validator service-side authentication. Because it is used to prevent malicious attacks, so directly throws an exception, and does not return the input page and output error message (if the Controller method has bindingresult parameters, it is left to the method inside the process, otherwise directly thrown out of the exception).
- In Spring-mvc.xml, add the definition of Hibernate validator
- Add @notblank definition to related attributes in User.java
- In the Userdetailcontroller Save method, add the @valid definition and the Bindingresult parameter.
7. WARNING: "Skipping URI variable ' ID ' since the request contains a bind value
The reason for this warning is that the URL path and the form have ID variables, and this time, it would be nice to change the URL path to another name.
@RequestMapping("Save/{userid}")
SPRINGMVC configuration causes real transaction invalidation