Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Problem With Url Mapping In Spring Hibernate Wb Application

springhibernate url tomcat spring-mvc hibernate

  • Please log in to reply
34 replies to this topic

#1 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 25 April 2012 - 09:00 AM

The spring/hibernate web application that I am creating is building and deploying correctly but I am having some problems when trying to access the jsp pages inside my WEB-INF/jsp folder. The test jsp page that I have placed in my WebContent folder opens correctly.
Here is my code:
new-customer.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
</head>
<body>

    <h1>Create New Customer</h1>

    <c:url var="saveUrl" value="/mis/start/newcustomer" />
    <form:form modelAttribute="customerAttribute" method="POST" action="${saveUrl}">
        <table>
            <tr>
                <td><form:label path="customerTargetId">Customer Target ID:</form:label></td>
                <td><form:input path="customerTargetId"/></td>
            </tr>

            <tr>
                <td><form:label path="customerName">Customer Name</form:label></td>
                <td><form:input path="customerName"/></td>
            </tr>

            <tr>
                <td><form:label path="customerCountry">Customer Country</form:label></td>
                <td><form:input path="customerCountry"/></td>
            </tr>
        </table>

        <input type="submit" value="Save" />
    </form:form>

</body>
</html>

start.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Test</h1>


<c:url var="addUrl" value="/mis/start/newcustomer" />
<p><a href="${addUrl}">Create new customer</a></p>
</body>
</html>

CustomerController
package testapp.mis.controller;

@Controller
@RequestMapping("/start")
public class CustomerController {

@Resource(name="customerService")
private CustomerService customerService;

@RequestMapping(value="/newcustomer", method=RequestMethod.GET)
public String getCustomer(Model model) {

    model.addAttribute("customerAttribute", new Customer());

    return "new-customer";
}

@RequestMapping(value="/newcustomer", method=RequestMethod.POST)
public String postCustomer(@ModelAttribute("customerAttribute") Customer customer) {
    customerService.createCustomer(customer);

    return "redirect:/mis/start";
}
}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
      <web-app id="WebApp_ID" version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

     <servlet>
     <servlet-name>mis</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
     </servlet>

<servlet-mapping>
<servlet-name>mis</servlet-name>
<url-pattern>/mis/*</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:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
	  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	  http://www.springframework.org/schema/context
	  http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
   

 <context:annotation-config />
 <context:component-scan base-package="testapp.mis" />
 <mvc:annotation-driven />
 <import resource="hibernate-context.xml" />
 
 </beans>

hibernate-context.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:p="http://www.springframework.org/schema/p"
	    xmlns:tx="http://www.springframework.org/schema/tx"
	    xmlns:context="http://www.springframework.org/schema/context"
	    xsi:schemaLocation="
           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
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
	  ">
 
 <context:property-placeholder location="/WEB-INF/spring.properties" />
     
    <!-- Enables annotations for transaction management -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    
    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
         p:dataSource-ref="dataSource"
         p:configLocation="${hibernate.config}"
         p:packagesToScan="testapp.mis"/>
 
 <!-- Declare a datasource that has pooling capabilities-->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"
    p:driverClass="${app.jdbc.driverClassName}"
    p:jdbcUrl="${app.jdbc.url}"
    p:user="${app.jdbc.username}"
    p:password="${app.jdbc.password}"
    p:acquireIncrement="5"
    p:idleConnectionTestPeriod="60"
    p:maxPoolSize="100"
    p:maxStatements="50"
    p:minPoolSize="10" />
 
 <!-- Declare a transaction manager-->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
		  p:sessionFactory-ref="sessionFactory" />
     
</beans>


hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
   
<hibernate-configuration>
  <session-factory>
 
   <!-- We're using MySQL database so the dialect needs to MySQL as well-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <!-- Enable this to see the SQL statements in the logs-->
    <property name="show_sql">false</property>
    
    <!-- Remove after testing -->
    
    <!-- This will drop our existing database and re-create a new one.
	  Existing data will be deleted! -->
    <property name="hbm2ddl.auto">create</property>
  </session-factory>
</hibernate-configuration>

mis-servlet.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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
	  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Declare a view resolver -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
	  p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

When I for example try to go to http://localhost/MIS/mis/start it tells me the page does not exist. (I have tried all combinations of localhost/MIS/start, localhost/mis/start/newcustomer etc.)



Can anyone see what the problem is?
Let me know if you need some other part of my code to help. Thank you!
  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 25 April 2012 - 10:57 PM

The url should be localhost(:port)/contextpath/dispatcherservlet/start/newcustomer
So propably http://localhost/MIS...art/newcustomer in your case.

Can you verify whether you enter the controller or not?
  • 0

#3 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 26 April 2012 - 12:57 AM

Thanks for your reply wim DC. I'm thinking that it's not entering the controller, how would I verify that and what can I do to fix the problem in that case? I have some doubts about whether its connecting to the database as well. I have been googling for the past two days trying to figure it out but no luck so I'm very grateful for your help
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 26 April 2012 - 12:58 AM

Breakpoint in the controller, or System.our.println anything inside the method.
  • 0

#5 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 26 April 2012 - 01:39 AM

okay I tried both but from what I can see the controller isn't getting called. Why could that be?
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 26 April 2012 - 01:45 AM

Copy the whole browser exception please. There are a few options:
- Dispatcherservlet not being called. errors like:
HTTP Status 404 - /dispatcherServletPath/x/y/z

- dispatcher can't find controller class. errors like:
HTTP Status 404 -

- dispatcher servlet can't find the method , but DOES find the class. errors like
HTTP Status 405 - Request method 'GET' not supported
  • 0

#7 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 26 April 2012 - 01:51 AM

This is what i get:

HTTP Status 404 -

type Status report
message
description The requested resource () is not available.
  • 0

#8 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 26 April 2012 - 01:55 AM

Okaay, so the dispatcherservlet works but it doesn't know anythign about your controller.

Likely because ApplicationContext.xml isn't mentioned anywhere so it's never loaded.

Try adding this to web.xml:
<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>/path/application-context.xml</param-value>
    </context-param>
    <listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

  • 0

#9 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 26 April 2012 - 02:25 AM

I added what you said to the web.xml. Now I'm getting a slightly different error:

HTTP Status 404 - /mistest/mis/start/newcustomer

type Status report
message /mistest/mis/start/newcustomer
description The requested resource (/mistest/mis/start/newcustomer) is not available.
Apache Tomcat/7.0.8


EDIT: I have created a new project with just the customer to simplify the application and focus on just getting the add customer function working (not product, delivery etc). thats why the contextpath is changed to "mistest." Everything else is exactly the same.
  • 0

#10 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 26 April 2012 - 02:40 AM

That appeirs as if the dispatcher servlet isn't working now. Are you sure the server started up fine? (No stacktraces?)

(I assume you changed the dispatcher servlet mapping to mistest then)
<url-pattern>/mistest/</url-pattern>

  • 0

#11 dlinx90

dlinx90

    CC Regular

  • Member
  • PipPipPip
  • 34 posts

Posted 26 April 2012 - 02:50 AM

actually there were a few errors that came up later:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/mistest/WebContent/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/mistest/WebContent/WEB-INF/applicationContext.xml]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4600)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5097)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5092)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/mistest/WebContent/WEB-INF/applicationContext.xml]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:118)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 20 more
Apr 26, 2012 11:22:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Apr 26, 2012 11:22:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/mistest] startup failed due to previous errors
Apr 26, 2012 11:22:38 AM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Apr 26, 2012 11:22:38 AM org.apache.catalina.core.StandardContext listenerStop
SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1066)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1040)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:988)
at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:541)
at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:142)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4640)
at org.apache.catalina.core.StandardContext$4.call(StandardContext.java:5326)
at org.apache.catalina.core.StandardContext$4.call(StandardContext.java:5305)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Apr 26, 2012 11:22:48 AM org.apache.catalina.core.ApplicationContext log
INFO: HTMLManager: list: Listing contexts for virtual host 'localhost'

  • 0

#12 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 26 April 2012 - 03:07 AM

Well that's pretty clear, it doesn't find your applicationcontext.xml file.

note that the path starts from the webcontent folder.
So if it was inside WEB-INF it would be:
<param-value>/WEB-INF/application-context.xml</param-value>

  • 0





Also tagged with one or more of these keywords: springhibernate, url, tomcat, spring-mvc, hibernate

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download