.Jsp mapping for controller in Spring

I am just starting with spring, so I started with SpringMVC on Heroku. Having selected the spring MVC pattern in Eclipse, I got a very basic application. Now I am trying to change this.

However, if I create another .jsp , and I visit the URL specified in the controller, I get 404, which is pretty clear because I never matched the controller with .jsp . But how do I do this?

Here is all that I changed (even if I'm not sure if you need it):

Controller:

 @Controller public class BookingController { @Autowired BookingService bookingService; @RequestMapping("/AvailableBikes") public String getAvailableBikes(Model model){ // TOOD: Fix Date int availableBookings = bookingService.getAvailableBookings(Calendar.getInstance().getTime()); model.addAttribute("NumAvailableBikes", Integer.toString(availableBookings)); return "NumAvailableBikes"; } } 

Essence:

 @Entity public class Booking { @Id @GeneratedValue private Integer id; // Lots of getters setters an attributes ... } 

Service:

 @Service public class BookingServiceImpl implements BookingService { @Override public void addBooking(Booking booking) { // TODO Auto-generated method stub } @Override public int getAvailableBookings(Date bookingDay) { // TODO Auto-generated method stub return 12; } @Override public void removeBooking(Booking booking) { // TODO Auto-generated method stub } } 

booking.jsp:

 <!doctype html> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <html> <head> <meta charset="utf-8"> <title>Spring MVC and Hibernate Template</title> <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet"> <link href="http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css" rel="stylesheet"> </head> <body> Test! <h2>${NumAvailableBikes}</h2> </body> 

web.xml:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Spring-Hibernate-Template</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>spring</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/booking/*</url-pattern> </servlet-mapping> </web-app> 

applicationContext.xml:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.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"> <context:annotation-config /> <context:component-scan base-package="org.stuttgart.fahrrad" /> <mvc:annotation-driven/> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <tx:annotation-driven /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="dataSource" ref="dataSource"/> </bean> <beans profile="default"> <jdbc:embedded-database id="dataSource"/> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean> </beans> <beans profile="prod"> <bean class="java.net.URI" id="dbUrl"> <constructor-arg value="#{systemEnvironment['DATABASE_URL']}"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + @dbUrl.getPath() }"/> <property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/> <property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <!-- change this to 'verify' before running as a production app --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> </beans> </beans> 
+4
source share
2 answers

return "NumAvailableBikes"; should be changed to return the "reservation";

+3
source

First, consider how your dispatcher's servlet is configured. It is currently mapped to the following URL pattern:

 <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/booking/*</url-pattern> </servlet-mapping> 

This means that only URLs containing the booking directory immediately after the root of the context will be displayed. Therefore, a URL such as `contextroot / AvailableBikes' will never be received and processed by the dispatcher. I would recommend changing your url template:

 <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 

This mapping serves as an almost complete mapping. It will match all URLs that do not display on another servlet. Please note: this will also create the need for configuration to map static resources such as css and js files. This is explained in the Spring documentation.

After you make this change, I think you can hit the controller with a URL like rootcontext/AvailableBikes . Configure the debug point in the controller to confirm. Also note that you will not use the URL containing the direct path to the .jsp file, but the path specified when matching the requests. Your jsp file should be accessible @ webcontent directory/WEB-INF/jsp/NumAvailableBikes.jsp .

In order to display booking.jsp , which should be accessible @ webcontent dir/WEB-INF/jsp/booking.jsp , the controller must be modified to return the reservation String .

  @RequestMapping("/AvailableBikes") public String getAvailableBikes(Model model){ // TOOD: Fix Date int availableBookings = bookingService.getAvailableBookings(Calendar.getInstance().getTime()); model.addAttribute("NumAvailableBikes", Integer.toString(availableBookings)); return "booking"; } 
+2
source

All Articles