【问题标题】:Error (http status 500) when i use Spring with tiles当我使用带有瓷砖的 Spring 时出现错误(http 状态 500)
【发布时间】:2014-09-27 18:33:23
【问题描述】:

我创建了一个项目 spring + tiles 但我总是得到这个错误。我阅读了很多指南但似乎无法解决问题。我哪里做错了?谢谢

tiles.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="template" template="/WEB-INF/tiles/classic.jsp">
<put-attribute name="title" value="Tiles tutorial homepage" />
<put-attribute name="header" value="/WEB-INF/tiles/banner.jsp" />
<put-attribute name="menu" value="/WEB-INF/tiles/common_menu.jsp" />
<put-attribute name="body" value="/WEB-INF/tiles/home_body.jsp" />
<put-attribute name="footer" value="/WEB-INF/tiles/credits.jsp" />
</definition>

<definition name="home" extends="template">
<put-attribute name="content" value="/WEB-INF/jsp/index.jsp"/>
</definition>
</tiles-definitions>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVCTiles</display-name>

<servlet>
 <servlet-name>hellotiles</servlet-name>
 <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
 <servlet-name>hellotiles</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

hellotiles-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

 <context:component-scan base-package="coreservlets"/>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
  </bean>

  <bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" >
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list> 
    </property>
  </bean>

</beans>

HelloController.java

package coreservlets;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {

@RequestMapping(value="/hello",method=RequestMethod.GET)
public String stampaHello(ModelMap model){
    model.addAttribute("messaggio","Ciao scusate il ritardo");
    return "hello";
}//stampaHello

@RequestMapping(value="/",method=RequestMethod.GET)
public String visualizzaIndex(){
    return "index";
}//visualizzaIndex

}//HelloController

你好.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${messaggio}</h1>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <a href="/SpringMVCTiles/hello">Stampa ciao</a>
</body>
</html>

【问题讨论】:

  • ViewResolver 应该处理 index 名称吗?它还没有在瓷砖上定义..所以感觉到错误。
  • 谢谢,我在tiles.xml中用name="index"替换了name="home",我解决了问题
  • 我来分享一下怎么回复。

标签: spring spring-mvc jakarta-ee tiles apache-tiles


【解决方案1】:

ViewResolver 应该如何处理 index 名称?它尚未在 tiles.xml 上定义。所以感觉错误

你的@Controller返回两个视图名称,它们是helloindex,但是你的tiles.xml 定义只有一个,它是 home

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2017-02-24
    • 1970-01-01
    相关资源
    最近更新 更多