I've tried looking at several similar questions on StackOverflow but couldn't really understand! This is my first Spring application, so I'm sorry for a possibly duplicate question. Any help would be really appreciated.
I'm getting the following error when I navigate to enterDetails page:-
Exception thrown by application class 'org.springframework.web.servlet.DispatcherServlet.render:1029'
javax.servlet.ServletException: Could not resolve view with name 'hotels/booking/enterBookingDetails' in servlet with name 'travel'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1029)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1290)
at [internal classes]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:207)
at [internal classes]
Here's the controller:-
@RequestMapping(value = "hotels/booking", method = RequestMethod.GET)
public String bookingDetails(@RequestParam Long hotelId, Model model) {
Booking booking = bookingService.createBooking(hotelId,"keith");
model.addAttribute(booking);
return "hotels/booking/enterBookingDetails";
}
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Configures the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Configures a handler for serving static resources by forwarding to the Servlet container's default Servlet. -->
<default-servlet-handler />
<!-- Maps view names to Tiles Definitions with support for partial re-rendering -->
<beans:bean id="viewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
<beans:property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTilesView"/>
</beans:bean>
<!-- Initializes the Apache Tiles CompositeView system -->
<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<beans:property name="definitions">
<beans:value>
/WEB-INF/**/tiles.xml
</beans:value>
</beans:property>
</beans:bean>
<!-- Configures Spring Web FLow
<beans:import resource="webflow.xml" /> -->
<!-- Configures transaction management around @Transactional components -->
<tx:annotation-driven />
<!-- Imports the application controllers that process client requests -->
<beans:import resource="controllers.xml" />
</beans:beans>
The 'FROM' Page:-
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<h1>${hotel.name}</h1>
<address>
${hotel.address}
<br />
${hotel.city}, ${hotel.state}, ${hotel.zip}
<br />
${hotel.country}
</address>
<form action="booking" method="get">
<p>
Nightly Rate:
<spring:bind path="hotel.price">${status.value}</spring:bind>
</p>
<input type="hidden" name="hotelId" value="${hotel.id}" />
<div>
<button type="submit">Book Hotel</button>
</div>
</form>
enterBookingDetails.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<div id="bookingForm">
<div class="span-5">
<h3>${booking.hotel.name}</h3>
<address>
${booking.hotel.address}
<br/>
${booking.hotel.city}, ${booking.hotel.state}, ${booking.hotel.zip}
<br/>
${booking.hotel.country}
</address>
<p>
Nightly rate: <spring:bind path="booking.hotel.price">${status.value}</spring:bind>
</p>
</div>
.
.
.
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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Enables use of HTTP methods PUT and DELETE -->
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Secures the application
<filter>
<filter-name>springSecurity</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>springSecurityFilterChain</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springSecurity</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>travel</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/travel/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>travel</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
booking/tiles.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
"http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
<definition name="enterBookingDetails" extends="standardLayout">
<put-attribute name="body" value="/WEB-INF/views/hotels/booking/enterBookingDetails.jsp"/>
</definition>
<definition name="reviewBooking" extends="standardLayout">
<put-attribute name="body" value="/WEB-INF/views/hotels/booking/reviewBooking.jsp" />
</definition>
</tiles-definitions>
I am new here, but I have been working on some spring MVC projects. So I hope to contribute to solving your problem.
I would change your controller to be able to work with the ModelAndView object, so you can do the following:
@RequestMapping(value = "hotels/booking", method = RequestMethod.GET)
public ModelAndView bookingDetails(@RequestParameter Long hotelId) {
ModelAndView model = new ModelAndView("enterBookingDetails");
Booking booking = bookingService.createBooking(hotelId,"keith");
model.addObject("booking", booking);
return model;
}