简体   繁体   English

通过Spring MVC Controller动作,如何获得对请求/响应的访问?

[英]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.

相关问题 如何将请求发送到另一个域并在Spring MVC控制器中获取响应正文? - How do I send a request to another domain and get the response body in a Spring MVC controller? Java:Spring MVC-如何在控制器操作中访问更新的模型? - Java: Spring MVC - How do I access the updated model in controller action? 从控制器(Spring MVC)异步获取响应 - Get response from controller (spring mvc) asynchronously 如何使用Spring MVC从Java中的控制器而不是通过Web浏览器请求HTML视图? - How Do I Request an HTML View from a Controller in Java and not through a Web Browser using Spring MVC? 如何从 Spring MVC 中同一控制器中不同 JSP 页面上的表单访问值? - How do I access values from forms on different JSP pages in the same controller in Spring MVC? 如何在Spring MVC中访问另一个控制器中一个控制器的请求参数? - How to access the request parameter of one controller in a different controller in Spring MVC? 如何将Freemarker模板中表单输入的数据导入spring MVC控制器? - How do I get data from the form inputs in a Freemarker template into a spring MVC controller? Spring控制器获取请求/响应 - Spring controller get request/response 如何在Spring MVC中从控制器访问bean? - How to access bean from controller in Spring MVC? Spring MVC - 如何在 Spring 控制器的地图中获取所有请求参数? - Spring MVC - How to get all request params in a map in Spring controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM