Java 如何在 Spring-mvc 中使用 html 链接调用控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24403672/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:00:55  来源:igfitidea点击:

How to invoke a controller using a html link in Spring-mvc?

javajavascriptjspspring-mvc

提问by Aki008

I have a jsp page called reports.jsp and I have displayed the links in the view for a user to click. How can I invoke Spring controller method by clicking on the link that will pass an argument.

我有一个名为reports.jsp 的jsp 页面,我在视图中显示了供用户单击的链接。如何通过单击将传递参数的链接来调用 Spring 控制器方法。

采纳答案by George Ant

You have to use @PathVariablein order to do this. Example:

你必须使用@PathVariable才能做到这一点。例子:

Jsp:

jsp:

<a href="<c:url value="/test/${object.argument}" />" >hello</a>

Controller:

控制器:

@RequestMapping(value = "/test/{argument}", method = RequestMethod.GET)
    public String Controller(@PathVariable("argument") String argument) {
       ...
    }

回答by Aki008

I have resolved the answer by creating a link:

我已经通过创建链接解决了答案:

<a href=".../test?argName=arg1" >hello</a>

Controller:

控制器:

@RequestMapping(value = "/test", method = RequestMethod.GET, params = {"argName"})
    public String Controller(@RequestParam(value="argName", required = true, defaultValue = null) String argName) {
       ...
       //Now do exciting things with variable argName
    }