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

Jaanus picture Jaanus · Aug 10, 2011 · Viewed 13.1k times · Source

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

Answer

Ralph picture Ralph · Aug 10, 2011

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.