Java、Spring、Apache Tiles 错误:无法解析名称为“spring”的 servlet 中名称为“contact”的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7013043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:16:45  来源:igfitidea点击:

Java, Spring, Apache Tiles error : Could not resolve view with name 'contact' in servlet with name 'spring'

javaspringapache-tiles

提问by Jaanus

controller:

控制器:

@Controller
@SessionAttributes
public class ContactController {

    @RequestMapping(value = "/addContact", method = RequestMethod.POST) 
    public String addContact(@ModelAttribute("contact") 
                            Contact contact, BindingResult result) {

         System.out.println("First Name:" + contact.getFirstName() +
                 "Last Name:" + contact.getLastName());

     return "redirect:contacts.html";
    }

    @RequestMapping("/contact")
    public ModelAndView showContacts() {

        return new ModelAndView("contact", "command", new Contact());
    }
}

this is my tiles.xml:

这是我的tiles.xml:

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

    <definition name="contact" extends="base.definition">
        <put-attribute name="title" value="Contact Manager" />
        <put-attribute name="body" value="/WEB-INF/jsp/contact.jsp" />
    </definition>

error is this:

错误是这样的:

org.apache.jasper.JasperException: javax.servlet.ServletException: Could not resolve view with name 'contact' in servlet with name 'spring'
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

feel free to ask for any more source code

随意索取更多源代码

回答by Ralph

In your last questionyou showed that you used a UrlBasedView Resolver with an pre and suffix.

上一个问题中,您表明您使用了带有前缀和后缀的 UrlBasedView 解析器。

Remove that pre and suffix.

删除该前缀和后缀。

That should be enogth:

那应该是:

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
    id="tilesViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
    id="tilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/layouts/layouts.xml</value>
            <!-- Scan views directory for Tiles configurations -->
            <value>/WEB-INF/views/**/views.xml</value>
        </list>
    </property>
</bean>

btw: this configruation allowes two tzps (only there sermatic differs) of tiles configuration files

顺便说一句:这个配置允许瓷砖配置文件的两个tzps(只有sermatic不同)

  • the /WEB-INF/layouts/layouts.xmlcontains all the (lets call it) "base definitions"
  • the /WEB-INF/views/**/views.xmlcontains the concrete definitions that extends the "base definitions" (for example the "contact" definition) -- You can have a seperate view.xmlfor each folder -- this is usefull if you group your views, for example all views (create, update, show and list) for each entity, in a separete folder
  • /WEB-INF/layouts/layouts.xml包含了所有的(可以称之为)“基本定义”
  • /WEB-INF/views/**/views.xml包含扩展“基本定义”(例如“联系人”的定义)的具体定义-你可以有一个单独的view.xml每个文件夹-这是有用的,如果你组的意见,例如,所有的意见(创建,更新, 显示和列出) 每个实体, 在一个单独的文件夹中

If you do not need this feature, then simply remove that line.

如果您不需要此功能,则只需删除该行即可。