我在SampleController中有以下代码;
@Controller
public class SampleController {
@RequestMapping("home")
public String loadHomePage(Model m) {
m.addAttribute("name", "CodeTutr");
return "home";
}
@RequestMapping(value="/test", method=RequestMethod.GET)
public String handlePost(@RequestParam String action, Model m) {
if( action.equals("save") ){
//handle save
}
else if( action.equals("renew") ){
//handle renew
}
m.addAttribute("name", "change");
return "home";
}
}
Run Code Online (Sandbox Code Playgroud)
在页面上加载我在网页上显示成功的属性.我试图让我的头围绕弹簧mvc下面的按钮点击是我的jsp代码;
<!DOCTYPE HTML>
<html>
<head>
<title>Sample Application</title>
</head>
<body>
<h1>Hello, ${name}!</h1>
<input type="submit" name="action" value="save" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的输入没有做任何事情,方法handlePost永远不会被命中.我试图将属性"名称"更改为"更改"这个词,我不确定我做错了什么.
你的问题不是Spring,而是HTML.您无法提交按钮.你只能提交一份<form>
.
将你的<input>
元素包裹在一个<form>
<form action="<c:url value="/test" />" method="GET">
<input type="submit" name="action" value="save" />
</form>
Run Code Online (Sandbox Code Playgroud)
taglib <c:url>
的url
标签在哪里core
.现在,当您单击该按钮时,浏览器会将您的<input>
元素序列化为url编码的表单参数并发送它们.它们将作为服务器/ Web应用程序的请求参数出现.Spring将按名称反序列化它们,并将它们作为参数注入,其中有一个@RequestParam
方法参数.