Matt
Matt

Reputation: 6963

Java: Spring MVC 3. Getting 404 errors when trying to map querystring parameters to method parameters

BACKGROUND

I come from .NET background and Spring MVC is new to me. In general, I'm not great with Java, so please answer in a way that's easy for even a Java newbie to understand.

PROBLEM

I am trying to build a simple CRUD controller. I am getting the main page working fine (View All), but then I keep getting 404 errors on pages where I am using @RequestParam to try map query string values to method parameters.

MAIN VIEW

<html>
    <head>
        <title>Users - KSC Technology & Sciences</title>
        <jsp:include page="../headParts.jsp" />
    </head>
    <body>
        <jsp:include page="../topMenu.jsp" />
        <div id="body" class="container-fluid">
            <div class="row-fluid">
                <div class="span3">
                    <jsp:include page="../leftMenu.jsp" />
                </div>
                <div class="span9">
                    <div class="row-fluid">
                        <a href="/users/create" class="btn btn-primary">Create</a>
                        <hr />
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>User Name</th>
                                    <th>E-mail</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <c:forEach var="row" items="${records}">
                                    <tr>
                                        <td><c:out value="${row.id}" /></td>
                                        <td><c:out value="${row.userName}" /></td>
                                        <td><c:out value="${row.email}" /></td>
                                        <td>
                                            <a href="/users/edit?id=${row.getId()}" class="btn">Edit</a>
                                            <a href="/users/delete?id=${row.getId()}" class="btn btn-danger">Delete</a>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <hr />
        <jsp:include page="../footer.jsp" />
    </body>
</html>

CONTROLLER

@Controller
public class UserController {

    @EJB(mappedName="java:global/KSC/UserService!com.ksc.services.UserService")
    private UserService service;

    @RequestMapping(value = "/users")
    public String index(ModelMap model) {
        model.addAttribute("records", service.findAll());
        return "user/index";
    }

    @RequestMapping(value = "/users/create")
    public String create(ModelMap model) {
        model.addAttribute("record", new KCSUser());
        return "user/edit";
    }

    @RequestMapping(value = "/users/edit")
    public String edit(ModelMap model, @RequestParam(value="id") String id) {
        int userId = Integer.parseInt(id);
        model.addAttribute("record", service.find(userId));
        return "user/edit";
    }

    @RequestMapping(value = "/users/delete")
    public String delete(ModelMap model, @RequestParam(value="id") String id) {
        int userId = Integer.parseInt(id);
        KCSUser user = service.find(userId);
        service.remove(user);

        return index(model);
    }
}

I can access the main view, but when clicking Edit (for example), then I get a 404. What am I doing wrong?

EDIT

Directory Structure:

enter image description here

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>
</web-app>

dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <context:component-scan base-package="com.ksc.controllers" />
    <mvc:annotation-driven />
</beans>

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>

Now that I think about it, I did have to hack the web.xml and dispatcher-servlet.xml files a bit because the ones generated by Netbeans didn't work.. so i copied it from some online source instead. I didn;t change the applicationContext.xml though and I see it is targetting Spring 2.5.. I'm confused.. can someone give me working config files for Spring 3.1? Maybe that's the problem here?

EDIT 2

Got it working thanks to @RC. The only thing I had to change with his answer was to remove the @RequestMapping from the class and have them on the methods only.

Upvotes: 1

Views: 1751

Answers (1)

user180100
user180100

Reputation:

The problem is @RequestParam this annotation tells spring MVC to look for the parameters in the request (ie. form elements with names). Better to use a template here:

@Controller
@RequestMapping("/users")
public class UserController {

    @EJB(mappedName="java:global/KSC/UserService!com.ksc.services.UserService")
    private UserService service;

    @RequestMapping(value = "")
    public String index(ModelMap model) {
        model.addAttribute("records", service.findAll());
        return "user/index";
    }

    @RequestMapping(value = "/create")
    public String create(ModelMap model) {
        model.addAttribute("record", new KCSUser());
        return "user/edit";
    }

    @RequestMapping(value = "/edit/{id}")
    public String edit(ModelMap model, @PathVariable("id") int userId) {
        KCSUser user = service.find(userId);
        // Assert.notNull(user, "user was not found");
        model.addAttribute("record", user);
        return "user/edit";
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(ModelMap model, @PathVariable("id") int userId) {
        KCSUser user = service.find(userId);
        // Assert.notNull(user, "user was not found");
        service.remove(user);

        return index(model);
    }
}

Documentation here

And in main view:

<a href="${pageContext.request.contextPath}/users/create" class="btn btn-primary">Create</a>
<!-- ... -->
<a href="${pageContext.request.contextPath}/users/edit/${row.id}" class="btn">Edit</a>
<a href="${pageContext.request.contextPath}/users/delete/${row.id}" class="btn btn-danger">Delete</a>

EDIT:

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd>
</beans>

Your problem is the mapper you use I think, can you remove <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> and try again?

Upvotes: 1

Related Questions