0

我正在尝试渲染视图。我正在使用 apache tails、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>

用户控制器.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";

    }


}

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

用户详细信息.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>
4

1 回答 1

0

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

@ResourceMapping("/users/{id} .html ") ...

于 2015-02-24T09:00:47.023 回答