4 Replies - 4994 Views - Last Post: 07 January 2010 - 11:59 AM

#1 jadeite100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-June 07

Spring Mvc 404 error

Post icon  Posted 03 January 2010 - 11:29 PM

Hi:

I took a course on Spring 2.56 from Interface21 and the labs I got get it to work during the training.

But when I tried it at home it doesn't work.
I go to the following url and it's find.
http://localhost:8080/accounts/

It display "This is my JSP page. " This is from the default index.jsp page.
When I tried the url as suggested in the lab "http://localhost:8080/accounts/accountSummary.htm"

It gives me a 404 error.

Note, the Tomcat 6 console didnot show any errors meaning it didn't invoke the code for the url.
I am using Myeclipse 8 on Tomcat6.
Here is my Spring information:

Web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="
			[url=http://java.sun.com/xml/ns/j2ee]http://java.sun.com/xml/ns/j2ee[/url]
			[url=http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"[/url]
		version="2.4">

	<!-- Beans in these files will makeup the configuration of the root web application context -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/accounts-application-config.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Deploys the 'accounts' dispatcher servlet whose configuration resides in /WEB-INF/accounts-servlet-config.xml -->
	<servlet>
		<servlet-name>accounts</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/accounts-servlet-config.xml</param-value>
		</init-param>
	</servlet>

	<!-- Maps all /accounts URLs to the 'accounts' servlet -->
	<servlet-mapping>
		<servlet-name>accounts</servlet-name>
		<url-pattern>/accounts/*</url-pattern>
	</servlet-mapping>

</web-app>


Accounts-servlet-config.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
			  [url=http://www.springframework.org/schema/beans/spring-beans-2.5.xsd]http://www.springframework.org/schema/bean...g-beans-2.5.xsd[/url]
			  [url=http://www.springframework.org/schema/context]http://www.springframework.org/schema/context[/url]
			  [url=http://www.springframework.org/schema/context/spring-context-2.5.xsd">]http://www.springframework.org/schema/cont...5.xsd">[/url]

	<!-- 
		The configuration for your 'accounts' Dispatcher Servlet.
		
		This example uses component scanning to automatically
		pick up controllers. 
			- Dependencies of controllers are wired using @Autowired support.
			- The URI scheme is controller using @RequestMapping annotations
	-->
	
	<context:component-scan base-package="accounts.web"/>
	
	<!--  
		if you would not have used component scanning you would have had
		to wire up the controller yourself:
		
		<bean class="accounts.web.AccountController">
			<constructor-arg ref="accountManager"/>
		</bean>
	 -->	
	
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
</beans>


accounts-application-config.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:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
							[url=http://www.springframework.org/schema/beans/spring-beans-2.5.xsd]http://www.springframework.org/schema/bean...g-beans-2.5.xsd[/url]
							[url=http://www.springframework.org/schema/tx]http://www.springframework.org/schema/tx[/url]
							[url=http://www.springframework.org/schema/tx/spring-tx-2.5.xsd]http://www.springframework.org/schema/tx/spring-tx-2.5.xsd[/url]
							[url=http://www.springframework.org/schema/context]http://www.springframework.org/schema/context[/url]
							[url=http://www.springframework.org/schema/context/spring-context-2.5.xsd">]http://www.springframework.org/schema/cont...5.xsd">[/url]

	<!-- The configuration of the account manager application. -->
	
	<context:annotation-config/>

	<!-- Weaves in transactional advice around @Transactional methods -->
	<tx:annotation-driven transaction-manager="transactionManager" />

	<!-- The account manager service that can load accounts from the database -->
	<bean id="accountManager" class="accounts.internal.HibernateAccountManager">
		<constructor-arg ref="sessionFactory" />
	</bean>

	<!-- A Hibernate SessionFactory for mapping Accounts and Restaurants from object to relation tables -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingLocations">
			<list>
				<value>classpath:accounts/internal/Account.hbm.xml</value>
				<value>classpath:accounts/internal/Beneficiary.hbm.xml</value>
			</list>
		</property>
	</bean>

	<!-- Creates an in-memory "rewards" database populated with test data for fast testing -->
	<bean id="dataSource" class="accounts.testdb.TestDataSourceFactory">
		<property name="testDatabaseName" value="rewards"/>
		<property name="schemaLocation" value="classpath:accounts/testdb/schema.sql"/>
		<property name="testDataLocation" value="classpath:accounts/testdb/test-data.sql"/>
	</bean>

	<!-- Drives transactions using Hibernate APIs when requested -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
		
	<!-- Translates Hibernate exceptions to Spring Data Access Exceptions -->
	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
	
</beans>


Here is the AccountControler.java:
package accounts.web;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import accounts.Account;
import accounts.AccountManager;

/**
 * A Spring MVC @Controller controller handling requests for both the
 * account summary and the account details pages. The accountDetails()
 * method return an account, corresponding to a given entity id. The
 * accountSummary() method returns a list with all accounts.  
 */
@Controller
public class AccountController {
	
	private AccountManager accountManager;
	
	/**
	 * Creates a new AccountController with a given account manager.
	 */
	@Autowired public AccountController(AccountManager accountManager) {
		this.accountManager = accountManager;
	}
	
	
	/**
	 * The @RequestMapping annotation takes care of setting the URL
	 * this controller will react this.
	 */
	@RequestMapping("/accountDetails.htm")
	public ModelAndView accountDetails(HttpServletRequest request) 
	throws ServletRequestBindingException {
		long id = ServletRequestUtils.getRequiredLongParameter(request, "entityId");
		ModelAndView mav = new ModelAndView("accountDetails");
		mav.addObject("account", accountManager.getAccount(id));
		return mav;
	}
	
	@RequestMapping("/accountSummary.htm")
	public ModelAndView accountSummary() {
		List<Account> accounts = accountManager.getAllAccounts();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("accountSummary");
		mav.addObject("accounts", accounts);
		return mav;
	}
}



The database is hsqldb.

Any help or hint would be greatly appreciated it.

Yours,

Frustrated.

Is This A Good Question/Topic? 0
  • +

Replies To: Spring Mvc 404 error

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Spring Mvc 404 error

Posted 03 January 2010 - 11:32 PM

Um...:code:

No one can read your code as-is, would you mind fixing it?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Spring Mvc 404 error

Posted 04 January 2010 - 12:38 AM

Huh ???
Was This Post Helpful? 0
  • +
  • -

#4 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Spring Mvc 404 error

Posted 04 January 2010 - 05:37 AM

As the others mentioned, you'd make our life easier by using the "code" tag...

At first glance, everything seems fine.

Let's get rid of the obvious first: Do you have the corresponding views under WEB-INF/views "accountSummary.jsp" and "accountDetails.jsp" ? Is there any error in the Tomcat console that would prevent the app to load properly?
Was This Post Helpful? 0
  • +
  • -

#5 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Spring Mvc 404 error

Posted 07 January 2010 - 11:59 AM

Could be a lot of things. It's hard to tell if your xml files are even correct because you've mixed bbcode in with them. I do see a potential issue though. Your servlet is named "accounts" in your web.xml file, yet you've named your config file "Accounts-servlet-config.xml." Since you haven't used the contextConfigLocation parameter to explicitly this file, then Spring will look under WEB-INF for a file named {yourServletName}-servlet.xml. So you can rename your file as "accounts-servlet.xml" or use the contextConfigLocation parameter to specify the current name, like so:

<init-param>
	 <param-name>contextConfigLocation</param-name>
	 <param-value>/WEB-INF/accounts-servlet-config.xml,/WEB-INF/Accounts-servlet-config.xml</param-value>
</init-param>

When Tomcat starts up, you should see the following in the console output.

Quote

INFO: Initializing Spring FrameworkServlet 'accounts'

If you're not seeing that then your servlet is not getting loaded.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1