Java:Spring MVC 3.在尝试将查询字符串参数映射到方法参数时出现404错误

时间:2012-12-17 02:42:53

标签: java spring-mvc

背景

我来自.NET背景,而Spring MVC对我来说是新手。一般来说,我对Java并不擅长,所以请以一种即使是Java新手也很容易理解的方式回答。

问题

我正在尝试构建一个简单的CRUD控制器。我正在使主页工作正常(查看全部),但随后我在使用@RequestParam尝试将查询字符串值映射到方法参数的页面上不断收到404错误。

主要观点

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

我可以访问主视图,但是当点击编辑(例如),然后我得到404.我做错了什么?

修改

目录结构:

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>

调度-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>

现在我考虑一下,我确实需要破解web.xml和dispatcher-servlet.xml文件,因为Netbeans生成的文件不起作用..所以我从一些在线源代码中复制了它。我没有改变applicationContext.xml,我看到它正在针对Spring 2.5 ..我很困惑..有人可以给我工作配置文件的Spring 3.1吗?也许这就是问题所在?

编辑2

感谢@RC的工作。我唯一要改变的是他的答案就是从课堂上删除@RequestMapping并将它们放在方法上。

1 个答案:

答案 0 :(得分:1)

问题是@RequestParam这个注释告诉spring MVC在请求中查找参数(即带有名称的表单元素)。最好在这里使用模板:

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

文档here

在主视图中:

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

修改

的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>

您的问题是我使用的映射器,您可以删除<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>并重试吗?