first, how to pass parameters
Use @RequestParam to pass query parameters. Example: http://localhost:8092/category/detail?id=1
@RequestMapping ("/detail") public String Detail (@RequestParam ("id") int id,model Model) { Category category= New Category (); Category.setcateid (ID); Category.setcatename ("Test Classification" +id); Model.addattribute ("Cate", category); return "detail.html"; }
Use @PathVariable to pass path parameters. Example: HTTP://LOCALHOST:8092/CATEGORY/EDIT/1
@RequestMapping (value = "/edit/{id}", method = requestmethod.get) public String edit (@PathVariable ("id") int ID, Model model) { //todo:get category from DB category category=new category (); Category.setcateid (ID); Category.setcatename ("Test Classification" +id); Model.addattribute ("Cate", category); return "edit.html"; }
Second, check the form
1. First define the entity class.
public class category{public Category () {} @NotNull @Min (1) private int cateid; @NotNull private String catename; public int Getcateid () { return cateid; } public void Setcateid (int cateid) { This.cateid = Cateid; } Public String Getcatename () { return catename; } public void Setcatename (String catename) { this.catename = Catename; }}
2. Form edit.html
<form method= "POST" th:object= "${cate}" th:action= "@{/category/save}" enctype= "Multipart/form-data" > <table> <tr> <td>id:</td> <td><input type= "text" th:field= "*{cateid } "></td> </tr> <tr> <td>name:</td> <td><input type=" Text "th:field=" *{catename} "></td> </tr> <tr> <td colspan=" 2 "> < Input type= "Submit" value= "Submission" > </td> </tr> </table> </form>
3. By adding @valid annotations to the parameters of the action method, this informs spring that it needs to ensure that the object satisfies the validation limit
@RequestMapping (value = "/save", method = requestmethod.post) public String Save (@Valid category category, Errors Errors) t Hrows IOException {...}
The error can be accessed through the Errors object, which is now used as a parameter to the Processregistration () method. (It is important to note that the errors parameter is immediately followed by the parameter with the @valid annotation, and @Valid note is the parameter to be examined.)
third, upload pictures
1. Setting up the Web. XML configuration
Web. XML Configuration Multipart-config
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</ load-on-startup> <multipart-config> <location></location> <max-file-size >2097152</max-file-size> <max-request-size>4194304</max-request-size> </ Multipart-config> </servlet>
2.from form
form to set the Enctype property to Multipart/form-data, which tells the browser to submit the form as multipart data
The input tag sets the type to file, which allows the user to select the image files to upload. The Accept property is used to restrict file types to JPEG, PNG, and GIF pictures. According to its Name property, the picture data will be sent to the profilepicture part in the multipart request
<form method= "POST" th:object= "${cate}" th:action= "@{/category/save}" enctype= "Multipart/form-data" > <table> <tr> <td>id:</td> <td><input type= "text" th:field= "*{cateid } "></td> </tr> <tr> <td>name:</td> <td><input type=" Text "th:field=" *{catename} "></td> </tr> <tr> <td>file:</td> <td> <input type= "file" accept= "image/jpeg,image/png,image/jpg" name= "Picture" > </ td> </tr> <tr> <td colspan= "2" > <input type= "Submit" value= "Submit" > </td> </tr> </table> </form>
3.controller:
@RequestPart: The image corresponds to the parameter to add the note
Spring provides the multipart Multipartfile object, which provides a richer object for processing multipart data
TransferTo (), which can help us to write the uploaded file to the file system
@RequestMapping (value = "/save", method = requestmethod.post) public String Save (@RequestPart ("Picture") Multipartfile picture, @Valid category category, Errors Errors) throws IOException { //todo:save file to Image server< C2/>string Filepath=request.getrealpath ("/") + "upload/" +picture.getoriginalfilename (); Picture.transferto (New File (filepath)); if (Errors.haserrors ()) { return "edit.html"; } Todo:save category to DB return "redirect:/category/detail?id=" +category.getcateid (); }
Getting Started with Java [17]-form form, uploading files