简体   繁体   English

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

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

I have a jsp page called reports.jsp and I have displayed the links in the view for a user to click.我有一个名为reports.jsp 的jsp 页面,我在视图中显示了供用户单击的链接。 How can I invoke Spring controller method by clicking on the link that will pass an argument.如何通过单击将传递参数的链接来调用 Spring 控制器方法。

You have to use @PathVariable in order to do this.您必须使用@PathVariable才能做到这一点。 Example:例子:

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) {
       ...
    }

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
    }

On the JSP page在 JSP 页面上

<a class="opcion"  href="<%= request.getContextPath()%>/inicio">Inicio</a>

And in the controller part backend并在控制器部分后端

@Controller
public class HomeController {
    @RequestMapping(value = "/inicio", method = RequestMethod.GET)
    public String index(ModelMap model){
        model.addAttribute("message", "cargaGeneracion");
        return "index";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM