Spring Boot Web 应用程序不响应帖子 - 只获取
我正在尝试让我的网络控制器方法响应获取和帖子。它响应 get,但我在发布时收到错误。我删除了除基本代码之外的所有内容。
这是我的控制器
@Controller
public class TestController {
@GetMapping(value = "/testForm")
public @ResponseBody String form1(
HttpSession session,
@RequestParam(name = "fName", required = false) String fName,
@RequestParam(name = "lName", required = false) String lName,
HttpServletResponse response
) {
response.addHeader("X-Frame-Options", "deny");
String html;
html = "<html><body>get form</body></html>";
return html;
}
@PostMapping(value = "/testForm")
public @ResponseBody String form2(
HttpSession session,
@RequestParam(name = "fName", required = false) String fName,
@RequestParam(name = "lName", required = false) String lName,
HttpServletResponse response
) {
response.addHeader("X-Frame-Options", "deny");
String html;
html = "<html><body>post form</body></html>";
return html;
}
这是我的 HTML
<html><body>
<form action="http://localhost:8080/testForm" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Post">
</form>
<form action="http://localhost:8080/testForm" method="get">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Get">
</form>
</body></html>
正如预期的那样,当我按下 Get 按钮时,我的控制器返回页面
get form
当我按下 post 按钮时,Spring 给出一个错误:
WARN 29588 --- [nio-8080-exec-7] o.s.w.s.r.ResourceHttpRequestHandler :
Path with "WEB-INF" or "META-INF": [WEB-INF/views/layouts/mainLayout.jsp]
我认为这个应用程序配置一定有一些错误 - 我接管现有的应用程序 - 这样可能会解释错误消息。但为什么我的 post 方法没有被调用?
这是非常奇怪的事情。我承认,我是 Spring 的新手 - 我是一个老 servlet 人员。我将相同的代码放入我维护的另一个 Spring Boot 应用程序中,这次它可以工作了。
这次,get方法输出明文,因此它在浏览器上显示为HTML标签。并且 post 方法也被调用,文本以 HTML 标签的形式出现。
那么有人可以解释一下是什么导致了这些差异吗?我怀疑有不同的配置。但这两个类都是@controller,并且都有完全相同的代码。
我可以分享其他可以提供线索的文件吗?谢谢。
I'm trying to get my web controller methods to respond to gets and posts. It responds to a get, but I get an error when posting. I removed all but the basic code.
Here is my controller
@Controller
public class TestController {
@GetMapping(value = "/testForm")
public @ResponseBody String form1(
HttpSession session,
@RequestParam(name = "fName", required = false) String fName,
@RequestParam(name = "lName", required = false) String lName,
HttpServletResponse response
) {
response.addHeader("X-Frame-Options", "deny");
String html;
html = "<html><body>get form</body></html>";
return html;
}
@PostMapping(value = "/testForm")
public @ResponseBody String form2(
HttpSession session,
@RequestParam(name = "fName", required = false) String fName,
@RequestParam(name = "lName", required = false) String lName,
HttpServletResponse response
) {
response.addHeader("X-Frame-Options", "deny");
String html;
html = "<html><body>post form</body></html>";
return html;
}
Here is my HTML
<html><body>
<form action="http://localhost:8080/testForm" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Post">
</form>
<form action="http://localhost:8080/testForm" method="get">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Get">
</form>
</body></html>
As expected, when I press the Get button, my controller returns the page
get form
When I press the post button, Spring gives an error:
WARN 29588 --- [nio-8080-exec-7] o.s.w.s.r.ResourceHttpRequestHandler :
Path with "WEB-INF" or "META-INF": [WEB-INF/views/layouts/mainLayout.jsp]
I think there must be some error configured with this app - I'm taking over an existing app - so that might explain the error message. But why isn't my post method being called?
Here's something really odd. I admit, I'm new to Spring - I'm an old servlet guy. I put this same code in another Spring Boot app I maintain, and this time it worked - sort of.
This time, the get method output plaintext, so it showed on the browser as HTML tags. And the post method also was called, and the text came out as HTML tags.
So could someone explain what could be causing these differences? I suspect there are configs that are different. But both classes were @controller, and both have the exact same code.
Are there other files I could share that would give a clue? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想在控制器方法中返回文本或 JSON,则应该在控制器类上添加
@RestController
或在方法上添加@ResponseBody
。如果不添加此注释,spring mvc默认返回重定向页面而不是文本或JSON
if you just want to return a text or JSON in the controller method, you should add the
@RestController
on your controller class or add@ResponseBody
on your method.if not add this annotation, the spring mvc default is to return the redirected page instead of text or JSON