jeudi 18 juin 2015

Rest/SpringMVC style mapping in Servlets

I always write Java web application in simple MVC way. For example I have a front contoller servlet mapped as /controller. then I send "action" parameter in query string to redirect/forward the request for processing.

Pages: main.jsp, users.jsp, reports.jsp Servlet: ControllerServlet - /controller

In home jsp page links for every menu:

<a href="<c:url value="/controller?action=main" />">Main</a>
<a href="<c:url value="/controller?action=users" />">Users</a>
<a href="<c:url value="/controller?action=reports" />">Reports</a>

And in the Controller servlet I get this paramter from the query string and process the request..

String action = request.getParameter("action");

switch(action) {
    case Constants.ACTION_MAIN:
        // processing for main and forward to main.jsp

    case ...:
        // etc

    default:
        // redirect to logout, etc
}

But it looks some way ugly, and I want to make my links like in Spring MVC with pathvariables and so. How must I redesign servlets structure, mapping, etc. to get this work done?

Aucun commentaire:

Enregistrer un commentaire