title: Mapping a .jsp to a Controller in Spring
body: <p>I am just starting with spring so I started with a SpringMVC on Heroku. By choosing the Spring MVC Template in Eclipse I got a very basic Application. Now I am trying to modify this.</p>
  
  <p>However if I create another <code>.jsp</code> and I visit the URL given in the Controller I get an 404, that's pretty clear, because I never mapped the Controller to the <code>.jsp</code>. But how should I do this?</p>
  
  <p>Here is everything I changed (Even if I'm not sure that you will need it):</p>
  
  <p>The Controller:</p>
  
  <pre><code>@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";
      }
  
  }
  </code></pre>
  
  <p>The Entity:</p>
  
  <pre><code>@Entity
  public class Booking {
  
      @Id
      @GeneratedValue
      private Integer id;
  
  
  // Lots of getters setters an attributes ...
  
  }
  </code></pre>
  
  <p>The Service:</p>
  
  <pre><code>@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
      }
  
  }
  </code></pre>
  
  <p>booking.jsp:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;%@taglib uri="http://www.springframework.org/tags" prefix="spring"%&gt;
  &lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;Spring MVC and Hibernate Template&lt;/title&gt;
  
      &lt;meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  
      &lt;link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet"&gt;
      &lt;link href="http://twitter.github.com/bootstrap/assets/css/bootstrap-responsive.css" rel="stylesheet"&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  Test!
  
  &lt;h2&gt;${NumAvailableBikes}&lt;/h2&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;display-name&gt;Spring-Hibernate-Template&lt;/display-name&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
              org.springframework.web.servlet.DispatcherServlet
          &lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/booking/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>applicationContext.xml:</p>
  
  <pre><code>&lt;?xml  version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="org.stuttgart.fahrrad" /&gt;
  
      &lt;mvc:annotation-driven/&gt;
  
      &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven /&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
  
      &lt;/bean&gt;
  
      &lt;beans profile="default"&gt;
          &lt;jdbc:embedded-database id="dataSource"/&gt;        
          &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
              &lt;property name="dataSource" ref="dataSource"/&gt;
              &lt;property name="jpaVendorAdapter"&gt;
                  &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
              &lt;/property&gt;
              &lt;property name="jpaProperties"&gt;
                  &lt;props&gt;
                      &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.HSQLDialect&lt;/prop&gt;
                      &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt;
                  &lt;/props&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/beans&gt;
  
      &lt;beans profile="prod"&gt;
          &lt;bean class="java.net.URI" id="dbUrl"&gt;
              &lt;constructor-arg value="#{systemEnvironment['DATABASE_URL']}"/&gt;
          &lt;/bean&gt;
  
          &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
              &lt;property name="url" value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + @dbUrl.getPath() }"/&gt;
              &lt;property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/&gt;
              &lt;property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/&gt;
          &lt;/bean&gt;
  
          &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
              &lt;property name="dataSource" ref="dataSource"/&gt;
              &lt;property name="jpaVendorAdapter"&gt;
                  &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
              &lt;/property&gt;
              &lt;property name="jpaProperties"&gt;
                  &lt;props&gt;
                      &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt;
                      &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                      &lt;!-- change this to 'verify' before running as a production app --&gt;
                      &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                  &lt;/props&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
      &lt;/beans&gt;
  
  &lt;/beans&gt;
  </code></pre>