1

背景

我来自 .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
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。我做错了什么?

编辑

目录结构:

在此处输入图像描述

网页.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>

应用程序上下文.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从课堂上删除并只让它们出现在方法上。

4

1 回答 1

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>

编辑:

应用程序上下文.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"/>试吗?

于 2012-12-17T03:10:23.957 回答