ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:788回复:0

Spring basic MVC app not working

楼主#
更多 发布于:2016-05-12 14:42
I tried Spring MVC app as described in the book "Spring in Action 3rd Edition". Unfortunately, till now, I am not able to run the simplest app after wasting 6 hrs. I am always getting the "HTTP Status 404" error. I found many threads on this forum for "@RequestMapping not working in Spring" and now wondering whether I am following the right book or is there something very basic which has troubled so many fellow Spring learners. I tried running the MVC app in the book itself (Ch-7, spitter-web) but again, that app also didn't work. Looks like the author has not paid great attention in trying his own apps. For the sake of completeness, my web.xml is: <?xml version="1.0" encoding="UTF-8" ?>

<web-app 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"
    version="2.4">

<context-param>
    <description>locations of the Spring configuration files</description>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    </param-value>
</context-param>

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

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>


</web-app>
 My application-servlet.xml is: <?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:util="http://www.springframework.org/schema/util"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!--<start id="spring_component_scan" />-->
   <context:component-scan base-package="com.truality.demo.web.controller" />
   <!--<end id="spring_component_scan" />-->

   <!--<start id="mvc_annotatedcontrollers" />-->
   <mvc:annotation-driven/>
   <!--<end id="mvc_annotatedcontrollers" />-->

   <context:annotation-config />

   <!--<start id="mvc_resources"/>-->
   <mvc:resources mapping="/resources/**" location="/resources/" />
   <!--<end id="mvc_resources"/>-->

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


 </beans>
 And my Controller class is: package com.truality.demo.web.controller; import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

    @RequestMapping(value={"/home.htm"}, method=RequestMethod.GET)
      public String showHomePage(Map<String, String> model) {
        model.put("name","Google");
        return "home";
    }
 I am getting the 404 error always when I try accessing - http://localhost:8080/demo-prop-1-1/home.htm OR
- http://localhost:8080/demo-prop-1-1/home.jsp
- http://localhost:8080/demo-prop-1-1/home
- http://localhost:8080/demo-prop-1-1
 I am down to the point where I will start evaluating some other technology like PHP or ... if I could not solve this trivial issue. Please help. Also, Can someone point me to good Spring book which give working examples so that some one can learn it fast? Thanks a lot in advance.You have this <servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>
 It makes no sense for your DispatcherServlet, named application, to handle *.jsp requests. Spring, by default, and you, through configuration, have not provided a handler for JSP resources. Get rid of that servlet-mapping. You are then left with <servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
 where /* is a path mapping that matches everything. This means that, even though you removed the other <servlet-mapping>, this mapping will still be used to handle requests to any paths ending with .jsp (among others). This means that the resource resolved by your registered InternalResourceViewResolver, that is /WEB-INF/views/home.jsp, will also be handled by Spring's DispatcherServlet. But again, it does not have a handler for such a path. Instead, you need to let a Servlet container's JSP Servlet handle the request. You can do this by mapping your DispatcherServlet to / <servlet-mapping>
    <servlet-name>application</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
 which marks it as the default servlet. A default servlet will handle any requests that weren't matched by any other <servlet-mapping>. Most Servlet containers have a JSP Servlet mapped to *.jsp, so any requests for a resource with the extension *.jsp will be handled by that before your Spring DispatcherServlet even gets a shot at it. More details here: Spring: servlet-mapping -> url-pattern : /* working but can't display

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部