Not Found Page Spring



我正在尝试渲染视图。我正在使用apachetails、jetty和Spring。我在general.xml中映射了视图。当我点击表users上的id以查看单个用户的视图时,页面崩溃了。

通用.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
    <definition name="common" template="/WEB-INF/layouts/classic.jsp">
        <put-attribute name="footer" value="/WEB-INF/layouts/footer.jsp" />
    </definition>
    <definition name="index" extends="common">
        <put-attribute name="title" value="Java Blog Aggregator" />
        <put-attribute name="body" value="/WEB-INF/jsp/index.jsp" />
        <put-attribute name="current" value="index"></put-attribute>
    </definition>

    <definition name="users" extends="common">
        <put-attribute name="title" value="Users" />
        <put-attribute name="body" value="/WEB-INF/jsp/users.jsp" />
        <put-attribute name="current" value="users"></put-attribute>
    </definition>
    <definition name="user-detail" extends="common">
        <put-attribute name="title" value="User detail" />
        <put-attribute name="body" value="/WEB-INF/jsp/user-detail.jsp" />
        <put-attribute name="current" value="users"></put-attribute>
    </definition>

</tiles-definitions>

UserController.java

package cr.test.jba.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;
import cr.rgarcia.jba.service.UserService;

@Controller
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/users")
    public String users(Model model){
        model.addAttribute("users",userService.findAll());
        return "users";
    }
    @ResourceMapping("/users/{id}")
    public String detail(Model model, @PathVariable int id){
        model.addAttribute("user", userService.findOne(id));
        return "user-detail";
    }

}

部分用户.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
    <table class="table table-bordered table-hover table-striped">
        <thead>
            <tr>
                <th>ID</th>
                <th>User Name</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${users}" var="user">
                <tr>
                    <td><a href="<spring:url value="/users/${user.id}.html" />">
                            ${user.id} </a></td>
                    <td>${user.name}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>

user-detail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<h1> ${ user.name } </h1>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>Springapp</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.json</url-pattern>
        <url-pattern>*.xml</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

您是否尝试将".html"添加到请求路径:

@资源映射("/users/{id}.html")…

相关内容

  • 没有找到相关文章

最新更新