Java @RequestMapping与@PathVariable URI不同
您好,我有一个问题,我正在学习如何使用PathVariable传递值,我有一个带有输入文本和按钮的表单,当您按下按钮时,它会将您带到其他页面并显示值,但当我按下按钮时它不起作用它会将我带到此URL:Java @RequestMapping与@PathVariable URI不同,java,html,spring,spring-mvc,Java,Html,Spring,Spring Mvc,您好,我有一个问题,我正在学习如何使用PathVariable传递值,我有一个带有输入文本和按钮的表单,当您按下按钮时,它会将您带到其他页面并显示值,但当我按下按钮时它不起作用它会将我带到此URL: http://localhost:8080/appThyme/shoForm1.html?firstname=MyName&submit= 我得到了这个错误HTTP404-/appThyme/showForm1.html 但是如果我把这个URL:http://localhost:8080/appTh
http://localhost:8080/appThyme/shoForm1.html?firstname=MyName&submit=
我得到了这个错误HTTP404-/appThyme/showForm1.html
但是如果我把这个URL:http://localhost:8080/appThyme/respuesta/Myname
它可以工作它可以用我的名字显示我的页面,我如何只按按钮就可以工作,为什么当我按按钮时它会在我的URI中添加问号和等号
@Controller
public class HomeController {
@RequestMapping(value = "/form1", method = RequestMethod.GET)
public String showFormulario2(Model model) {
logger.info("***PAG formulario***");
return "form1.html";
}
@RequestMapping(value = "/showForm1/{id}", method = RequestMethod.GET)
public String showForm(Model model, @PathVariable("id") String id)
{
String theId= id;
model.addAttribute("TheID", theId);
return "showForm1.html";
}
我的form1.html页面
<form id="guestForm" th:action="@{/showForm1.html}" method="get">
<div>
<input type="text" name="firstname" id="firstname"></input>
</div>
<div>
<button type="submit" name="submit">Submit</button>
</div>
</form>
enter code here
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The value is ${nombre} </P>
</body>
</html>
提交
我的showForm1.html页面
<form id="guestForm" th:action="@{/showForm1.html}" method="get">
<div>
<input type="text" name="firstname" id="firstname"></input>
</div>
<div>
<button type="submit" name="submit">Submit</button>
</div>
</form>
enter code here
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The value is ${nombre} </P>
</body>
</html>
在此处输入代码
家
你好,世界!
值为${nombre}
表单提交不适用于此处使用的@PathVariable构造@PathVariable用于REST样式的URI,而这不是表单提交时生成的
如果将控制器签名更改为如下所示:
@RequestMapping("/showForm1.html", method = RequestMethod.GET)
public String showForm(Model model, @RequestParam("firstname") String id)
{
String theId= id;
model.addAttribute("TheID", theId);
return "showForm1.html";
}
然后在表单提交时应该正确调用该方法。为什么要将
th:action=“@{/showForm1.html}”
?应该如何处理?因为我使用的是thymeleaf视图分解器,“th”标记和注释是必需的,但如果我使用带有正常“action=”tagOk的正常视图分解器,我会遇到同样的问题,忘记该操作。为什么要使用该路径?我只是想学习如何使用PathVariable在页面之间读取过去的值,我想知道如何使用itRead读取您在RequestMapping
中输入的内容。它与您发布的URL不匹配。如果要从表单提交,可能需要一个查询参数而不是路径变量。