Aki008
Aki008

Reputation: 403

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. How can I invoke Spring controller method by clicking on the link that will pass an argument.

Upvotes: 1

Views: 32484

Answers (3)

Edson Suárez
Edson Suárez

Reputation: 31

On the JSP page

<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";
    }
}

Upvotes: 0

Aki008
Aki008

Reputation: 403

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
    }

Upvotes: 1

George Ant
George Ant

Reputation: 371

You have to use @PathVariable in order to do this. Example:

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

Upvotes: 7

Related Questions