[英]From a spring mvc controller action, how do I get access to request/response?
My action looks like: 我的动作如下所示:
public String add() {
return "/WEB-INF/views/add.jsp";
}
In this case I need access to a posted form field parameter. 在这种情况下,我需要访问已发布的表单字段参数。 Once I receive the posted parameter value, I will save it to the db and then return an http response code of 200 if there are no errors.
收到发布的参数值后,将其保存到数据库,如果没有错误,则返回http响应代码200。 If an error has occured, I will return HTTP 500 response code.
如果发生错误,我将返回HTTP 500响应代码。
How can I set the http response code in the return (instead of the view unless I can do both?). 我如何在返回中设置http响应代码(除非可以同时执行,否则将替换视图)。
Assuming this is a @RequestMapping
-annoted controller, then just declare the form field as a parameter: 假设这是一个
@RequestMapping
注释的控制器,然后只需将form字段声明为参数即可:
public String add(String myformFieldName) {
To return an explicit status code on the response, then declare the response and set it: 要在响应上返回明确的状态码,然后声明响应并进行设置:
public String add(String myformFieldName, HttpServletResponse httpResponse) {
httpResponse.setStatus(...);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.