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

时间:2014-06-25 08:25:34

标签: java javascript jsp spring-mvc

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

3 个答案:

答案 0 :(得分:7)

您必须使用@PathVariable才能执行此操作。例如:

JSP:

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

控制器:

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

答案 1 :(得分:1)

我通过创建链接解决了问题:

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

控制器:

@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
    }

答案 2 :(得分:0)

在JSP页面上

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

在控制器部分后端

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