spring mvc 404

hf201429 发布于 2015/07/23 15:14
阅读 469
收藏 0

小弟最近学习springmvc 刚开始搭建的时候出现个404请教各位大神:

1.项目文档结构:

2.web.xml:

<web-app version="2.5" 
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_2_5.xsd">
  <display-name>hftest</display-name>
  
     <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
    <servlet>  
   <servlet-name>hftest</servlet-name>  
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
   <load-on-startup>1</load-on-startup>  
    </servlet>
     <servlet-mapping>  
   <servlet-name>hftest</servlet-name>  
   <url-pattern>*.do</url-pattern>  
 </servlet-mapping>
 <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  

</web-app>


3.spring 配置文件:hftest-servlet.xml:

<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"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-3.1.xsd  
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util-3.1.xsd 
           http://www.springframework.org/schema/mvc   
           http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd" default-autowire="byName">  


<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  />
<mvc:annotation-driven />

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

<mvc:view-controller path="/" view-name="index" />

<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>  
        <!-- 配置jsp路径前缀 -->
        <property name="prefix" value="/"></property>  
        <!-- 配置URl后缀 -->
        <property name="suffix" value=".jsp"></property>  
    </bean>

</beans>

4.java 的 controller:

@Controller
public class ValidProjectController{

private Logger log = Logger.getLogger(getClass());

@RequestMapping("/valid/validProject")
public String validProject(){
log.info("this is a simple valid project ... ");
return "comm/validP";
}
}

在访问http://localhost:8080/hftest/valid/validProject.do 的时候网页状态为404

后台也没有log打出。

请教各位大大是不是我哪里配置出现了错误。






加载中
0
lijegd
lijegd
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/hftest-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
hf201429
hf201429
谢谢
0
lijegd
lijegd
@RequestMapping("/valid/validProject") 改为@RequestMapping("/valid/validProject.do")
0
hf201429
hf201429

引用来自“lijegd”的评论

@RequestMapping("/valid/validProject") 改为@RequestMapping("/valid/validProject.do")
不行啊。。。
lijegd
lijegd
你tomcat起来的时候有没有报错?
0
hf201429
hf201429

引用来自“lijegd”的评论

@RequestMapping("/valid/validProject") 改为@RequestMapping("/valid/validProject.do")

引用来自“hf201429”的评论

不行啊。。。

tomcat启动没有报错,

2015-7-23 15:33:08 org.apache.catalina.core.AprLifecycleListener init
信息: Loaded APR based Apache Tomcat Native library 1.1.24.
2015-7-23 15:33:08 org.apache.catalina.core.AprLifecycleListener init
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2015-7-23 15:33:08 org.apache.coyote.http11.Http11AprProtocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2015-7-23 15:33:08 org.apache.coyote.ajp.AjpAprProtocol init
信息: Initializing Coyote AJP/1.3 on ajp-8009
2015-7-23 15:33:08 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 447 ms
2015-7-23 15:33:08 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2015-7-23 15:33:08 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.33
2015-7-23 15:33:08 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor host-manager.xml
2015-7-23 15:33:08 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor manager.xml
2015-7-23 15:33:08 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory docs
2015-7-23 15:33:08 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory examples
2015-7-23 15:33:08 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2015-7-23 15:33:08 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2015-7-23 15:33:08 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory hftest
2015-7-23 15:33:11 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(E:\tomcat\apache-tomcat-6.0.33\webapps\hftest\WEB-INF\lib\servlet-api-2.4.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
2015-7-23 15:33:11 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'hftest'
2015-7-23 15:33:12 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory ROOT
2015-7-23 15:33:12 org.apache.coyote.http11.Http11AprProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2015-7-23 15:33:12 org.apache.coyote.ajp.AjpAprProtocol start
信息: Starting Coyote AJP/1.3 on ajp-8009
2015-7-23 15:33:12 org.apache.catalina.startup.Catalina start
信息: Server startup in 4016 ms

0
hf201429
hf201429

引用来自“lijegd”的评论

<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/hftest-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
不行。。。
0
hf201429
hf201429

是不是我hftest—servlet.xml配置有问题 ?


0
lijegd
lijegd
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext.xml</param-value>

</context-param>

再加上spring 的监听器,看看tomcat能不能正常起来,不行的话,帖log

0
hf201429
hf201429

引用来自“lijegd”的评论

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext.xml</param-value>

</context-param>

再加上spring 的监听器,看看tomcat能不能正常起来,不行的话,帖log

可以正常启动,但是依然访问404,

又有个问题我没有applicationContext.xml这个配置文件也可以正常启动 ?

很奇怪,我把context-param这个去掉,tomcat反而启动报错,说applicationContext。xml找不到。。。很奇怪,下面是正常启动的日志:

2015-7-23 15:47:44 org.apache.catalina.core.AprLifecycleListener init
信息: Loaded APR based Apache Tomcat Native library 1.1.24.
2015-7-23 15:47:44 org.apache.catalina.core.AprLifecycleListener init
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2015-7-23 15:47:44 org.apache.coyote.http11.Http11AprProtocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2015-7-23 15:47:44 org.apache.coyote.ajp.AjpAprProtocol init
信息: Initializing Coyote AJP/1.3 on ajp-8009
2015-7-23 15:47:44 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 416 ms
2015-7-23 15:47:44 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2015-7-23 15:47:44 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.33
2015-7-23 15:47:44 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor host-manager.xml
2015-7-23 15:47:44 org.apache.catalina.startup.HostConfig deployDescriptor
信息: Deploying configuration descriptor manager.xml
2015-7-23 15:47:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory docs
2015-7-23 15:47:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory examples
2015-7-23 15:47:44 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2015-7-23 15:47:44 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2015-7-23 15:47:44 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory hftest
2015-7-23 15:47:44 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(E:\tomcat\apache-tomcat-6.0.33\webapps\hftest\WEB-INF\lib\servlet-api-2.4.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
2015-7-23 15:47:45 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
2015-7-23 15:47:45 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'springMVC'
2015-7-23 15:47:45 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory ROOT
2015-7-23 15:47:45 org.apache.coyote.http11.Http11AprProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2015-7-23 15:47:45 org.apache.coyote.ajp.AjpAprProtocol start
信息: Starting Coyote AJP/1.3 on ajp-8009
2015-7-23 15:47:45 org.apache.catalina.startup.Catalina start
信息: Server startup in 967 ms

0
lijegd
lijegd

你的jar包都有吗

0
hf201429
hf201429
jar包没有会报错的。。。
返回顶部
顶部