java 通过 spring 控制器从 jsp 获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15089801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value from jsp via spring controller
提问by Martin Dvoracek
I've created MVC Template via Spring Tool Suite IDE
, but I can't realize, how to get values from jsp
. For exmaple - I've created text input in jsp
我已经通过 MVC 模板创建了 MVC 模板Spring Tool Suite IDE
,但我不知道如何从jsp
. 例如 - 我已经在jsp
<input type="text" />
but how to get the value to controller to be able to work with it there? I know, that when I add atribute to model in my controller, then I can access it via ${name}
, but how to do it the other way?
但是如何获得控制器的值以便能够在那里使用它?我知道,当我在控制器中向模型添加属性时,我可以通过 访问它${name}
,但如何以另一种方式访问它?
回答by Jean-Philippe Bond
You need a form and then submit the value to the controller ...
您需要一个表单,然后将值提交给控制器...
Something like :
就像是 :
<form name="foo" action="/foo/bar" method="post">
<input name="fieldName" type="text" />
<input type="submit" value="Submit">
<form>
And then get it in your controller :
然后在你的控制器中获取它:
@Controller
@RequestMapping(value = "/foo")
public class AdminController {
@RequestMapping(value = "/bar")
public String testAction(@RequestParam String fieldName) {
// yourValue contain the value post from the html form
return "yourview";
}
}