04_spring MVC annotation development

Posted by aissa on Thu, 12 Sep 2019 15:32:39 +0200

I. Annotation Development

Requirements: 1. Enter the commodity query list page.

2. Click on the modification and enter the commodity modification page. The page shows the commodities to be modified (query from the database), the commodities to be modified are queried from the database, and the commodity information is queried according to the commodity ID (primary key).

3. In the commodity modification page, modify the commodity information, after modification, click submit.

1.mapper Development

There are already mapper.xml and mapper.java generated by reverse engineering.

2.service Development

Functions: commodity inquiry, commodity information inquiry according to id, commodity information modification according to id, and commodity information deletion according to ID.

package com.ssm.service;

import com.ssm.po.ItemsCustom;
import com.ssm.po.ItemsQueryVo;

import java.util.List;

/**
 * Description:ItemsService
 * User: jiatp
 * Date:2019/9/7 0007 5:22 p.m.
*/

public interface ItemsService {
    //Commodity inquiry
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;

    //Inquire commodity information according to id
    public ItemsCustom findItemsById(int id)throws Exception;

    //Modify commodity information according to id
    public void updateItems(Integer id,ItemsCustom itemsCustom)throws Exception;
    //Delete merchandise information based on id
    public void deleteItems(Integer[] ids)throws Exception;


}

3. Develop controller

Functions: modify the display of pages, modify the submission of pages,

package com.ssm.controller;

import com.ssm.exception.CustomException;
import com.ssm.po.ItemsCustom;
import com.ssm.po.ItemsQueryVo;
import com.ssm.service.ItemsService;
import com.ssm.validation.ValidGroup1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * Description:
 * User: jiatp
 * Date:2019/9/7 0007 8:40 p.m.
*/

@Controller
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    //Commodity Information Query
    @RequestMapping("/queryItems")
    public ModelAndView queryItems(HttpServletRequest request, ItemsQueryVo itemsQueryVo) throws Exception{
        String id = request.getParameter("id");
        System.out.println(id);
        //Call service to look up the database and query the list of goods. Static simulation is used here.
        List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);

        //Return to Model AndView
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("itemsList",itemsList);//Equivalent to request.setAtttributes
        modelAndView.setViewName("items/itemsList");//Specify the returned view
        return modelAndView;
    }

    /**
     * @Author jiatp
     * @Date 2019/9/8 0008 12:21 p.m.
     * @MethodName
     * @MethodParameter
     * @MethodReturnType
     * @declare Commodity Modification Page Display
    */
//    @RequestMapping(value = "/editItems",method = {RequestMethod.GET,RequestMethod.POST})
//    public ModelAndView editItems() throws Exception{
//        // call service query according to id
//        ItemsCustom itemsCustom = itemsService.findItemsById(1);
//
//        // Define Model AndView
//        ModelAndView  modelAndView = new ModelAndView();
//        // Put commodity information in modelandView
//        modelAndView.addObject("itemsCustom",itemsCustom);
//        // Page presentation
//        modelAndView.setViewName("items/editItems");
//        return modelAndView;
//    }
    // The return value of the controller method is shown here as String
/*
    @RequestParam Specify request input parameters and parameters to bind
    required = true Does the specified parameter need to be passed in
    defaultvalue Default values can be set if the id parameter is not passed in, and the default values and parameters can be bound.
*/
    @RequestMapping(value = "/editItems",method = {RequestMethod.GET,RequestMethod.POST})
    public String editItems(Model model, @RequestParam(value = "id",required = true) Integer item_id) throws Exception{
        //Call service query according to id
        ItemsCustom itemsCustom = itemsService.findItemsById(item_id);

        model.addAttribute("itemsCustom",itemsCustom);
        return "items/editItems";
    }


    //Amendment submission of commodity information page,
    //  Add @Validated before validating pojo and BindingResult after validation to accept parameters for validation errors
    // These two pairs appear in a fixed order.
    @RequestMapping("/editItemsSubmit")
    public String editItemsSubmit(Model model, HttpServletRequest request, Integer id,ItemsCustom itemsCustom) throws Exception{
        //Other operations

        //Call service to update page information
        itemsService.updateItems(id,itemsCustom);
        //Redirect return to "forward: queryItems. action"
        return "success";
    }
}

Annotation @RequestMapping

Functions:

  1. url mapping: define the corresponding url of controller method for processor mapping.
  2. Narrowing request mapping: classifying url s, you can define the root path here, and the final access path is: root path + subpath

3. Restrict HTTP request methods: For security reasons, restrict http links. If the restriction request is post method, get request is made and error is reported:

It can be defined as:

Return value of controller method

1. Return to Model AndView: At the end of the method, define the Model AndView and set the model and view separately.

2. Return string: If the controller method returns string,

 

Redirect redirect: redirect redirect feature: the url in the browser address bar changes. Modifying the submitted request data cannot be passed to the redirected address. Because re-request after redirection (request cannot be shared)

Forward page forwarding: forward page forwarding, browser address bar url unchanged, request can be shared.

3. return to void

1. Use request to turn to the page, as follows: request.getRequestDispatcher("page path"). forward(request, response);

2.response page redirection: response.sendRedirect("url")

3. Specify the response results through response, such as response json data as follows:

response.setCharacterEncoding("utf-8");

response.setContentType("application/json;charset=utf-8");

response.getWriter().write("json string");

Testing...

Topics: Java Database JSON xml