gpt4 book ai didi

java - Spring Web MVC Tiles 属性 'header' 未找到

转载 作者:行者123 更新时间:2023-12-02 03:55:55 27 4
gpt4 key购买 nike

我正在尝试在我的 Spring MVC 项目中使用tiles库。

wel-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
xmlns:mvc="http://www.springframework.org/schema/mvc">

<context:annotation-config />
<context:component-scan base-package="com.smart" />
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="myTransactionManager" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>

<mvc:resources mapping="/resources/**" location="/resources/theme1/" />

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

</beans>

tiles.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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="login" template="/view/home.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/view/header.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="" />
</definition>
</tiles-definitions>

home.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<html>
<head>
<Title>Home</Title>
</head>
<body>
<tiles:insertAttribute name="header" />
</body>
</html>

ProfilePage.java

@Controller
public class ProfilePage {

@RequestMapping(value = "/login", method=RequestMethod.GET)
public ModelAndView showPost(@ModelAttribute("commentForm") Post post, ModelMap model, HttpSession session)
{
return new ModelAndView("home", model);
}

@RequestMapping(value = "/login", method=RequestMethod.POST)
public ModelAndView loginWorld(ModelMap model, @ModelAttribute("commentForm") Post post, @ModelAttribute("loginForm") Employee employee, HttpSession session, BindingResult errorResult){

// Logic

model.addAttribute("result", result);
return new ModelAndView(returnPage, model);
}
}

错误:

org.apache.jasper.JasperException: org.apache.tiles.template.NoSuchAttributeException: Attribute 'header' not found.
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

org.apache.tiles.template.NoSuchAttributeException: Attribute 'header' not found.
org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:50)
org.apache.tiles.template.InsertAttributeModel.resolveAttribute(InsertAttributeModel.java:165)
org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:121)
org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
org.apache.jsp.view.home_jsp._jspx_meth_tiles_005finsertAttribute_005f0(home_jsp.java:379)
org.apache.jsp.view.home_jsp._jspx_meth_c_005fif_005f0(home_jsp.java:318)
org.apache.jsp.view.home_jsp._jspService(home_jsp.java:147)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我不明白为什么会出现这个错误!

最佳答案

首先将“view”文件夹移动到“WEB-INF”目录并更改以下映射

尝试更改tiles.xml中的以下映射

<definition name="login" template="/view/home.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/view/header.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="" />
</definition>

至以下内容

<definition name="base.definition" template="/WEB-INF/view/template.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/WEB-INF/view/header.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="" />
</definition>


<definition name="home" extends="base.definition">
<put-attribute name="title" value="Welcome" />
<put-attribute name="body" value="/WEB-INF/view/home.jsp" />
</definition>

The return value of every controller will be mapped with each tiles definition which is associated with jsp to render as body in template. This is why we need to add mapping for 'home'.

创建template.jsp如下

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<html>
<head>
<Title>template</Title>
</head>
<body>
<tiles:insertAttribute name="header" />
</body>
</html>

然后是你的 home.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<html>
<head>
<Title>Home</Title>
</head>
<body>
</body>
</html>

在 home.jsp 中,您不需要插入属性“header”。瓷砖将为您完成此操作。

并删除InternalResourceViewResolver

关于java - Spring Web MVC Tiles 属性 'header' 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35447784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com