Spring MVC -- restful addition, deletion, modification and query


    2.2 objectives and methods. Use @ RequestMapping("/emps") to determine what requests to process. In the target method

                                Put the data found in the database into the map, and then spring MVC will put it into the request domain object.

                                The view parser will parse the return value and forward it to the specified target page.

public class EmployeeHandler {
	private EmployeeDao employeeDao;
	public String list(Map map) {
		map.put("employees", employeeDao.getAll());
		return "list";

3.RESTful addition

  3.1 requirements: display the add page. Then click the submit button to display all employee information pages and complete the addition operation.

                    Note: ① the URL s of both pages are emp. But the request method is different

                                ② There is data when the department of the added page is initialized, which means that when the added page arrives, it must pass through

                                    handler method to find the data, and then echo it to the page( That is, the drop-down list of the page during initialization

                                    Must have a value).


3.2 GET request to obtain the page information of adding employees

<a href="emp">Add New Employee</a>

3.3 Handler handles requests and target methods. The page needs to echo the value of departments, so it needs to be put into the request during initialization.

      At the same time, because of the form requirements of spring MVC, put a bean object corresponding to the form in the request.

 *@RequestMapping To determine which request the target method handles, two qualifications are used,
 * Because the URL is the same.
public String input(Map<String,Object> map){
	map.put("departments", departmentDao.getDepartments());
	map.put("employee",new Employee());
       return "input";

3.4 add page

        Can pass modelAttribute Property specifies the bound model properties,If this attribute is not specified, it defaults from request Read from domain object
        command Form for bean,If the attribute value does not exist, an error occurs.
         (in other words, SpringMVC Whether the page is loaded for the first time or not, you need to echo the page, which needs to be displayed in the request field
          There must be a corresponding form field in the object bean Object, if not specified, will be read by default command Form for bean
           If there is no, an error is reported, which requires that when requesting the page, you need to request Put a in the domain object bean)
       <form:form action="emp"  method="POST" modelAttribute="employee">
               LastName: <form:input path="lastName"/>
               Emali: <form:input path="email"/>
                   Map<String,String> genders = new HashMap();
                  request.setAttribute("genders", genders);
               <!-- You don't have to write at this time itemLabele and itemValue,Self defined resolution -->
               Gender: <form:radiobuttons path="gender" items="${genders}" />
               <!-- Get directly from the domain object because get On this page, departments Has been placed in the domain object-->
               Department: <form:select path="department.id" items="${departments}"  
                                    itemLabel="departmentName"  itemValue="id"></form:select>
                <input type="submit" value="Submit"> 

3.5 send a POST request and perform the add operation. The target method performs a redirect operation to the original all employees display page.

public String save(Employee employee){
       return "redirect:/emps";

4. Delete


  four point two   Send delete request.

<td><a  class="delete" href="emp/${emp.id}">Delete</a></td>

  However, it should be noted that the requirements are required to be deleted by DELETE, so it is necessary to convert the ordinary hyperlink GET request method to

          It is a POST request, which is converted into a DELETE request. Click the hyperlink below to trigger the click event and cancel the original GET request,

        Instead, send a POST request through the form, which involves the acquisition of static resources.

 <!-- Want to use restful send out delete Request, what must be sent is post,Configured HiddenHttpMethodFilter 
You can put post Request converted to delete or put Request, but the general hyperlinks are get Request, you need to use js-->
<td><a  class="delete" href="emp/${emp.id}">Delete</a></td>
<form action="" method="post">
        <input type="hidden" name="_method"  value="DELETE">
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
	$(function() {
		$(".delete").click(function() {
			var href = $(this).attr("href");
			$("form").attr("action", href).submit();		
			return false;

four point three   Call the target method. Note that @ RequestMapping is a DELETE request. Get the placeholder in the URL through the @ PathVariable annotation.

       After deletion, redirect to the target page.

public String delete(@PathVariable("id") Integer id){
       return "redirect:/emps";

Tags: Java RESTful

Posted on Fri, 03 Sep 2021 23:39:30 -0400 by delldeveloper