我已经配置了一个Spring MVC / Hibernate / Tiles应用程序。一切正常,直到我介绍了Tiles组件。我相信我做对了,但我一直得到“无法渲染空模板”异常
这是我的root-context.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<context:component-scan base-package="com.acts542.mygunshop" />
<mvc:interceptors>
<bean class="com.acts542.mygunshop.controller.SecurityInterceptor" />
</mvc:interceptors>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/tiles.xml</value>
</list>
</property>
</bean>
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mygunshop"/>
<property name="username" value="mygunshop"/>
<property name="password" value="sen32164"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan">
<array>
<value>com.acts542.mygunshop.model</value>
<value>com.acts542.mygunshop.dao</value>
<value>com.acts542.mygunshop.service</value>
<value>com.acts542.mygunshop.controller</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
这是我的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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是我的tile.xml
<!DOCTYPE tiles-definitions PUBLIC
"-//ApacheSoftwareFoundation//DTDTilesConfiguration2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="template" template="WEB-INF/tiles/mainLayout.jsp">
<put-attribute name="header" value="/WEB-INF/tiles/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/tiles/menu.jsp" />
<put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp" />
<put-attribute name = "content" value="/WEB-INF/views/Login.jsp" />
</definition>
<definition name = "Login">
<put-attribute name = "content" value="/WEB-INF/views/Login" />
</definition>
<definition name = "Home" extends="template">
<put-attribute name = "content" value="/WEB-INF/views/Home" />
</definition>
</tiles-definitions>
这是我的mainLayout.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">
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>This is a test!</title>
</head>
<body>
<table border="1" style="border-collapse: collapse;" cellpadding="2" cellspacing="2" align="center" width="100%">
<tbody>
<tr>
<td width="20%" rowspan="3">
<tiles:insertAttribute name="menu" />
</td>
<td width="80%" height="20%">
<tiles:insertAttribute name="header" />
</td>
</tr>
<tr>
<td width=80% height="60%">
<tiles:insertAttribute name="content" />
</td>
</tr>
<tr>
<td height="20%">
<tiles:insertAttribute name="footer" />
</td>
</tr>
</tbody>
</table>
</body>
</html>
LoginController.java
package com.acts542.mygunshop.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.acts542.mygunshop.dto.BusinessMessage;
import com.acts542.mygunshop.dto.LoginDto;
import com.acts542.mygunshop.form.LoginForm;
import com.acts542.mygunshop.service.LoginService;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
@Controller
@RequestMapping({"/", "/Login"})
public class LoginController
{
private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
LoginService loginService;
@Autowired
public void setLoginService(LoginService loginService)
{
this.loginService = loginService;
}
@RequestMapping(method = RequestMethod.GET)
public String processGet(Map<String, LoginForm> model)
{
LoginForm loginForm = new LoginForm();
model.put("form", loginForm);
return "Login";
}
@RequestMapping(method = RequestMethod.POST)
public String processPost(@Valid @ModelAttribute("form") LoginForm form, BindingResult result, HttpSession session)
{
if (result.hasErrors())
{
return "Login";
}
LoginDto loginDto = new LoginDto();
loginDto.setUserName(form.getUserName());
loginDto.setPassword(form.getPassword());
loginDto = loginService.loginEmployee(loginDto);
if(null == loginDto)
{
result.addError(new ObjectError("form", "User Not Found"));
result.addError(new ObjectError("form", "Please Try Again"));
return "Login";
}
if(loginDto.getMessages().hasMessages())
{
result.addError(new ObjectError("form", loginDto.getMessages().getMessage()));
List<BusinessMessage> messages = loginDto.getMessages().getMessages();
if(null != messages)
{
Iterator<BusinessMessage> iterator = messages.iterator();
while(iterator.hasNext())
{
result.addError(new ObjectError("form", iterator.next().getMessage()));
}
}
return "Login";
}
session.setAttribute("LoggedOnEmployee", loginDto);
return "redirect:/Home";
}
}
异常
SEVERE: Servlet.service() for servlet appServlet threw exception
org.apache.tiles.impl.InvalidTemplateException: Cannot render a null template
a t org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:51)
at org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:690)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:644)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:627)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:321)
at org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
at java.lang.Thread.run(Thread.java:662)
尝试了一切 - 除了你们可能会添加的内容。同样,一切似乎都正常工作和正确连线。如果登录呈现登录页面将表格中的用户/密码发布到数据库中,返回到数据库并呈现主页。一旦我插入Tiles,我就无法呈现第一页。
答案 0 :(得分:2)
是tiles.xml还是tile.xml?
在tiles.xml中,是否应该扩展模板定义的Login和Home定义?