CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2009
    Posts
    13

    Question Spring MVC - Url Handling Problem

    Hello to everyone,

    I'm trying to setup a Spring MVC project. I posted my configuration files below.
    I'll be really glad if someone helps me.

    Thanks in advance,
    With regards,
    Talha.

    web.xml
    Code:
    <?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>local</display-name>
      
    	<context-param>
    		<param-name>webAppRootKey</param-name>
    		<param-value>webapp.root</param-value>
    	</context-param>
    	<context-param>
    		<param-name>log4jConfigLocation</param-name>
    		<param-value>/WEB-INF/log4j.xml</param-value>
    	</context-param>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
        		/WEB-INF/applicationContext.xml</param-value>
    	</context-param>
    	
     	<listener>
        	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     	 </listener>    
        
    	<servlet>
    		<servlet-name>webmvc</servlet-name>
    		<servlet-class>
    			org.springframework.web.servlet.DispatcherServlet
    		</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>
    				/WEB-INF/webmvc-servlet.xml
    			</param-value>
    		</init-param>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
      
    	<servlet-mapping>
    		<servlet-name>webmvc</servlet-name>
    		<url-pattern>*.*</url-pattern>
    	</servlet-mapping>
    
    	<session-config> 
    		<session-timeout>30</session-timeout> 
    	</session-config> 
    
    	<filter>
    		<filter-name>encoding-filter</filter-name>
    		<filter-class>
                org.springframework.web.filter.CharacterEncodingFilter
    		</filter-class>
    		<init-param>
    			<param-name>encoding</param-name>
    			<param-value>UTF-8</param-value>
    		</init-param>
    		<init-param> 
    			<param-name>forceEncoding</param-name> 
    			<param-value>true</param-value> 
    		</init-param> 
    	</filter>
    
    	<filter-mapping>
    		<filter-name>encoding-filter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
        
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    		<welcome-file>index.htm</welcome-file>
    		<welcome-file>jsp/index.jsp</welcome-file>
    		<welcome-file>default.html</welcome-file>
    		<welcome-file>default.htm</welcome-file>
    		<welcome-file>jsp/default.jsp</welcome-file>
    		<welcome-file>login.html</welcome-file>
    		<welcome-file>login.htm</welcome-file>
    		<welcome-file>jsp/login.jsp</welcome-file>
    	</welcome-file-list>
      
    	<error-page>
    		<error-code>404</error-code>
    		<location>/404.jsp</location>
    	</error-page>
    	<error-page>
    		<exception-type>java.lang.Exception</exception-type>
    		<location>/error.jsp</location>
    	</error-page>
     
    </web-app>
    webmvc-servlet.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     	
     	<bean id="localControllerMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
    		<property name="prefix" value="hnd_"/>
    	</bean>
    	
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="viewClass"> 
    			<value>org.springframework.web.servlet.view.JstlView</value>
    		</property>	
    		<property name="prefix"> 
    			<value>/WEB-INF/jsp/</value>
    		</property>
    		<property name="suffix"> 
    			<value>".jsp"</value>
    		</property>
    	</bean>
    	
    	<bean id="localController" class="view.LocalVIEW">
    		<property name="methodNameResolver">
    			<ref local="localControllerMethodNameResolver"/>
    		</property>
    		<property name="localBUS">
    			<ref bean="localBUS"/>
    		</property>
    	</bean>
    	
    	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="mappings">
    			<props>
    				<prop key="/*.htm">localController</prop>
    				<prop key="/*.html">localController</prop>
    				<prop key="/*.jsp">localController</prop>
    				<prop key="/*.ajax">localController</prop>
    			</props>
    		</property>
    	</bean>
      
    	<bean id="localBUS" class="bus.LocalBUS">
    	</bean>
    	
    </beans>
    applicationContext.xml
    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="
    	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    	
    	<bean id="txProxyTemplate" abstract="true"	class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref local="transactionManager" />
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="list*">PROPAGATION_REQUIRED, readOnly</prop>
    				<prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
    				<prop key="*">	PROPAGATION_REQUIRED, -MVARollbackException,+MVANoRollbackException	</prop>
    			</props>
    		</property>
    	</bean>
    	
    
    	<bean id="contextApplicationContextProvider" class="context.ApplicationContextProvider"/>  
    	<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
    	
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref local="baseSessionFactory" />
    		</property>
    	</bean>
    	<!-- BUSINESS TARGET PRIMARY BUSINESS OBJECT: Hibernate implementation -->
    	
    	<bean id="localBUS" parent="txProxyTemplate" >
    		<property name="target">
    			<ref local="targetLocalBUS" />
    		</property>
    	</bean>
    	
    	
    	<bean id="targetLocalBUS" class="bus.LocalBUS">
    		<property name="localDAO">
    			<ref local="local1DAO" />
    		</property>
    	</bean>
    	
    	<bean id="hibernateConfiguration"
    		factory-bean="&amp;baseSessionFactory"
    		factory-method="getConfiguration" />
    
    	
    
    	<bean id="local1DAO" class="dao.LocalDAO" >
    		<property name="sessionFactory">
    			<ref local="baseSessionFactory" />
    		</property>
    	</bean>
    	
    	<bean id="baseSessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref local="dataSource" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    				<prop key="hibernate.show_sql">false</prop>
    			</props>
    		</property>
    	</bean>
    
    	
    	<bean id="propPlaceholderConfigurer" class="advice.PropResolver" init-method="init">
    		<property name="locationsMap">
    			<props>
    				<prop key="prod">/WEB-INF/prod.properties</prop>
    			</props>
    		</property>
    		<property name="ignoreUnresolvablePlaceholders" value="true"/>
    	</bean>
    	
    	<bean id="dataSource"	class="com.mchange.v2.c3p0.ComboPooledDataSource"	destroy-method="close">
    		<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
    		<property name="jdbcUrl">		
    			<value>jdbc:oracle:thin:@${database.url}</value>
    		</property>
    		<property name="properties">
    			<props>
    				<prop key="user">${database.user}</prop>
    				<prop key="password">${database.password}</prop>
    			</props>
    		</property>
    		<property name="maxPoolSize" value="50"/>
    		<property name="initialPoolSize" value="2"/>
    		<property name="minPoolSize" value="1"/>
    		<property name="maxStatements" value="200"/>
    		<property name="maxIdleTime" value="300"/>
    		<property name="acquireIncrement"  value="10"/>
    		<property name="unreturnedConnectionTimeout" value="90"/>
    		<property name="maxConnectionAge" value="120"/>
    	</bean>
    	
    </beans>
    And my Controller class:
    Code:
    package view;
    
    import java.io.IOException;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ConcurrentMap;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
    
    import bus.LocalBUS;
    
    public class LocalVIEW extends MultiActionController {
    
    	/** Logger for this class and subclasses */
    	protected final Log logger = LogFactory.getLog(getClass());
    
    	private static Log log = LogFactory.getLog(LocalVIEW.class);
    
    	protected LocalBUS localBUS;
    
    	public LocalBUS getLocalBUS() {
    		return localBUS;
    	}
    
    	public void setLocalBUS(LocalBUS localBUS) {
    		this.localBUS = localBUS;
    	}
    
    	@SuppressWarnings({ "rawtypes", "unchecked" })
    	public ModelAndView hnd_firstPage(HttpServletRequest request,
    			HttpServletResponse response) throws IOException {
    		try {
    			ConcurrentMap map = new ConcurrentHashMap();
    			map.put("ad", "TK");
    			logger.info("SpringappController - returning firstPage view...   TKTK");
    			return new ModelAndView("firstPage", map);
    		} catch (Exception e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
    
    }
    firstPage.jsp
    Code:
    <%@ 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>My First MVC Page</title>
    </head>
    <body>
    <p>This is my name : ${ad}</p>
    </body>
    </html>

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Spring MVC - Url Handling Problem

    Well, it's really hard to help you when all you post is your code and we're supposed to perform a "where's Waldo" on what the problem is

    Please tell us what problem you are having with the URL handling.

    As a side note, are you using an older version of Spring MVC and cannot use annotations (it doesn't look like it, but I can't see your dependency jar files)? Doing it the annotation way saves you a LOT of headaches in dealing with the XML. In my app-config.xml I have the following:

    Code:
    <?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: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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
        <!-- Scans within the base package of the application for @Components to configure as beans -->
        <context:component-scan base-package="com.foo.project" />
    </beans>
    This tells Spring MVC to look under the "com.foo.project" package for Controller classes with the @Controller annotation.

    My mvc-config.xml is similarly easy:

    Code:
    <?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
        <!-- Configures support for @Controllers -->
        <mvc:annotation-driven />
    
        <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
        
    </beans>
    Leaving me the beauty of using Spring MVC: very little xml config I just do my thing with annotations (limited at that).

    Here is one of my controller classes:
    Code:
    @Controller
    public class CalendarController {
        private Logger logger = org.slf4j.LoggerFactory.getLogger(CalendarController.class);
        
        @RequestMapping(value="/calendar.htm" ,method = RequestMethod.GET)
        public String getCalendar(Model model) {
            String ret = "calendar";
    
            ...snip...
            //adding a list to the response
            model.addAttribute("events",events);
            
            return ret;
        }
    }

  3. #3
    Join Date
    May 2009
    Posts
    13

    Thumbs up Re: Spring MVC - Url Handling Problem

    Thank you so much ProgramThis, I changed my approach to Annotation-based configuration. Really it's a lot easier than XML-based approach. But when I tried to deploy my project in eclipse, I saw an exception like this :

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.lang.IllegalArgumentException: Resource path [/home/talha/workspace/AnnoMVC/WebContent/WEB-INF/classes/myControllers] does not denote a directory

    Do you know how can I solve this problem?
    Thanks in advance..

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: Spring MVC - Url Handling Problem

    Not without seeing your configuration XML files (the new ones). Are you sure that the directory path / structure listed in the exception exists and you are able to navigate to it?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured