首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@PathVariable的@RequestMapping是不同的

@PathVariable的@RequestMapping是不同的
EN

Stack Overflow用户
提问于 2014-09-03 15:39:09
回答 1查看 580关注 0票数 0

嗨,我有一个问题,我正在学习如何用PathVariable传递值,我有一个带有输入文本和按钮的表单,当你按下按钮时,它会带你到另一个页面并显示这个值,但是当我按下这个按钮时,它就不起作用了。它带我到这个网址:

http://localhost:8080/appThyme/shoForm1.html?firstname=MyName&submit=

我得到了一个错误HTTP 404 - /appThyme/showForm1.html

但是,如果我把这个URL:http://localhost:8080/appThyme/respuesta/Myname工作它显示给我我的页面上有我的名字,我怎样才能让它只按下按钮,为什么当我按这个按钮时,它会给我的URI添加问号和相等的符号

代码语言:javascript
复制
 @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页面

代码语言:javascript
复制
<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>

我的howForm1.html页面

代码语言:javascript
复制
enter code here 
<html>
  <head>
    <title>Home</title>
  </head>
 <body>
<h1>
  Hello world!  
</h1>

 <P>  The value is  ${nombre} </P>

</body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2014-09-03 15:57:13

表单提交并不打算使用您在这里使用的@PathVariable构造。@PathVariable用于REST风格的URI,而这不是在表单提交上生成的。

如果您将控制器签名更改为如下所示:

代码语言:javascript
复制
@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";
}

然后,在表单提交时应该正确地调用该方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25648446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档