Java、Spring、Apache Tiles 错误:无法解析名称为“联系人”的视图;在名为“spring”的 servlet 中

发布于 2024-11-28 15:33:50 字数 1956 浏览 0 评论 0原文

控制器:

@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());
    }
}

这是我的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>

错误是这样的:

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)

随时询问更多源代码

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2024-12-05 15:33:50

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

删除该前缀和后缀。

这应该是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>

顺便说一句:此配置允许两个tzps(只有语义不同)的图块配置文件

  • /WEB-INF/layouts/layouts.xml包含所有(我们称之为)“基本定义”
  • /WEB-INF/views/**/views.xml 包含扩展“基本定义”的具体定义“(例如“联系人”定义) - 您可以为每个文件夹拥有一个单独的 view.xml - 如果您对视图进行分组,例如所有视图(创建、更新、显示和列表),这非常有用每个实体位于单独的文件夹中

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

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

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

  • the /WEB-INF/layouts/layouts.xml contains all the (lets call it) "base definitions"
  • the /WEB-INF/views/**/views.xml contains the concrete definitions that extends the "base definitions" (for example the "contact" definition) -- You can have a seperate view.xml for 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文