0

I think I've done everything right, but I still get 404's on all my pages (like /main/signup.htm). I'm kind of lost on what I could be missing here (the equivalent thing as xml configuration works, but I wanted to do this with annotations - this is an educational project and the rest of my team is even less familiar with spring and such than I am, so I thought doing at least some of the configuration in the java files would help ease them in).

web.xml

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


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

    <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>
            index.jsp
        </welcome-file>
    </welcome-file-list>

</web-app>

springapp-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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" >

    <mvc:annotation-driven />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

MainController(imports omitted):

@Controller
@RequestMapping("/main")
public class MainController {

    @RequestMapping(value="/default.htm", method=RequestMethod.GET)
    public String defaultAfterLogin(HttpServletRequest request) {
        if(request.isUserInRole("ADMIN")) {
            return "redirect:/main/admin.htm";
        } else {
            return "redirect:/main/signup.htm";
        }
    }
    @RequestMapping(value="/signup.htm", method=RequestMethod.GET)
    public ModelAndView signup() {
        return new ModelAndView("signup");
    }

    @RequestMapping(value="/admin.htm", method=RequestMethod.GET)
    public ModelAndView admin() {
        return new ModelAndView("admin");
    }

}

some relevant part of catalina.out:

INFO: Deploying web application archive /home/hannes/.tomcat/apache-tomcat-7.0.34/webapps/login_proto-1.0.0.war
Jan 21, 2013 6:33:15 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'springapp': initialization started
Jan 21, 2013 6:33:15 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'springapp-servlet': startup date [Mon Jan 21 18:33:15 CET 2013]; root of context hierarchy
Jan 21, 2013 6:33:15 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/springapp-servlet.xml]
Jan 21, 2013 6:33:15 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@499bb935: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,viewResolver]; root of factory hierarchy
Jan 21, 2013 6:33:15 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'springapp': initialization completed in 332 ms
Jan 21, 2013 6:33:22 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/login_proto-1.0.0/auth/login.htm] in DispatcherServlet with name 'springapp'
Jan 21, 2013 6:38:04 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/login_proto-1.0.0/main/default.htm] in DispatcherServlet with name 'springapp'
4
  • Is there anything defined in global pattern in file web.xml e.g. <url-pattern>/*</url-pattern> Commented Jan 21, 2013 at 17:48
  • @ShashiBhushan The web.xml is literally what I posted here, I didn't edit anything. Commented Jan 21, 2013 at 17:49
  • Please refer stackoverflow.com/questions/4305555/… .. Commented Jan 21, 2013 at 17:50
  • @ShashiBhushan That doesn't seem to be relevant to my problem - the user there just forgot to add the RequestMapping to his methods. Commented Jan 21, 2013 at 18:16

1 Answer 1

2

Add the <context:component-scan base-package="your.package.controller" /> tag to your springapp-servlet.xml

<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"
    xmlns:beans="http://www.springframework.org/schema/beans"
    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.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="your.package.controller" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>
Sign up to request clarification or add additional context in comments.

6 Comments

This is definitely a step in the right direction, but now I get "The matching wildcard is strict, but no declaration can be found for element 'context:component-scan'. org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)"
did you added the spring-context.jar to your project?
Yes, I got spring-context-3.2.0.RELEASE.jar as well as spring-context-support-3.2.0.RELEASE.jar (even though I'm not sure what the latter is for, I just added it to be safe).
did you copy the springapp-servlet.xml that I posted? It had an error try with the updated version
You mean the extra quotation marks? No, I didn't copy it, I just added the xmlns:context and xsi:schemaLocation entries.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.