SpringMVC: Handler parameters and return value

@RequestMapping

URL path mapping

@RequestMapping(value="/item")
或
@RequestMapping("/item")

value的值是数组,可以将多个url映射到同一个方法

Narrow request mapping

Add @RequestMapping (url) to the class to specify a common request prefix. Restrict all methods under this class. Request URL must start with the request prefix, and use this method to classify and manage the URL.

@RequestMapping放在类名上边,设置请求前缀:
@Controller
@RequestMapping("/item")

@RequestMapping放在方法名上边,设置请求映射url:
@RequestMapping("/queryItem")

访问地址为:/item/queryItem

Request method limitation

Limit the GET method, if you access via Post you will get an error: HTTP Status 405-Request method 'POST' not supported

@RequestMapping(value="/editItem", method=RequestMethod.GET)

Limit POST method, if you access via Post you will get an error: HTTP Status 405-Request method 'GET' not supported

@RequestMapping(method=RequestMethod.POST)

GET and POST can be

@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})

The return value of the controller method

Return to ModelAndView

When the method needs to end, define ModelAndView and set the model and view separately.

ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList", itemsList);
modelAndView.setViewName("itemsList.jsp");
return modelAndView;

Return void

The request and response can be defined on the formal parameters of the controller method, and use the request or response to specify the response result:

1. Use request to go to the page:

request.getRequestDispatcher("页面路径").forward(request, response); 

2. Redirect via response page:

response.sendRedirect("url") ;

3. You can also specify the response result through response, for example, response json data:

response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");

Return string

1. Logical view address

The controller method returns a string to specify the logical view address, which is resolved to the physical view address by the view resolver.

Physical view address (jsp path) = prefix + logical view address + suffix

<!-- springmvc.xml配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!-- 配置jsp路径的前缀 -->
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <!-- 配置jsp路径的后缀 -->
    <property name="suffix" value=".jsp"/>
</bean>

//Handler方法返回逻辑视图地址
return "items/itemsList";

2. Redirect

The controller method returns the result and redirects to a url address, the url in the browser address bar will change. Modified request data cannot be sent to the redirected address, because the request is re-executed after the redirect, and the request cannot be shared.

//重定向到queryItem.action地址,request无法带过去
return "redirect:queryItem.action"; 

The redirect method is equivalent to "response.sendRedirect ()". After forwarding, the browser's address bar becomes the forwarded address, because forwarding executes a new request and response.

Since the original parameters of a new request cannot be passed to the next url when forwarding, if you want to pass the parameters, you can add parameters after /item/queryItem.action, as follows: / item / queryItem? ... & ...

3. Forwarding

After the controller method is executed, continue to execute another controller method, forwarding the page through forward, the URL of the browser address bar remains unchanged, and the request can be shared.

//结果转发到editItem.action,request可以带过去
return "forward:editItem.action"; 

The forward mode is equivalent to "request.getRequestDispatcher (). forward (request, response)". After forwarding, the browser address bar is still the original address. Forwarding does not execute a new request and response, but shares a request and response with the request before forwarding. Therefore, the parameters requested before forwarding can still be read after forwarding.

Published 202 original articles · praised 37 · 30,000+ views

Guess you like

Origin blog.csdn.net/lovecuidong/article/details/103487002