开发者社区> 问答> 正文

spring mvc 返回404error

使用spring 搭建的mvc。

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/spring/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/applicationContext.xml
        </param-value>
    </context-param>
 
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>
 
</web-app>
applicationContext.xml:配置注解扫描路径,数据源和viewResolver
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
    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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
 
    <context:property-placeholder location="classpath:jdbc.properties" />
    <context:annotation-config />
    <context:component-scan
        base-package="org.yunzhong.comments.model.dao,
            org.yunzhong.comments.model.service,
            org.yunzhong.comments.controller,
            org.yunzhong.comments.filter" />
 
    <tx:annotation-driven />
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClass}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize">
            <value>1</value>
        </property>
        <property name="maxActive">
            <value>50</value>
        </property>
        <property name="maxWait">
            <value>10000</value>
        </property>
        <property name="minIdle">
            <value>1</value>
        </property>
        <property name="minEvictableIdleTimeMillis">
            <value>300000</value>
        </property>
        <property name="validationQuery">
            <value>SELECT 1</value>
        </property>
        <property name="testWhileIdle">
            <value>true</value>
        </property>
        <property name="timeBetweenEvictionRunsMillis">
            <value>60000</value>
        </property>
        <property name="testOnBorrow">
            <value>false</value>
        </property>
        <property name="testOnReturn">
            <value>false</value>
        </property>
    </bean>
 
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>org.yunzhong.comments.model.entity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext
                </prop>
            </props>
        </property>
    </bean>
 
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
 
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>
页面目录(不能上传图片,所以只能如下打出目录结构):

webapp:

        WEB-INF:

            pages:

                home.jsp

controller:

package org.yunzhong.comments.controller;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
/**
 * @author wangyzh
 * @date 2014年11月24日 下午2:29:47
 */
@Controller
public class FirstLoadController {
    private static final Log log = LogFactory.getLog(FirstLoadController.class);
 
    @RequestMapping(value = "/home")
    public ModelAndView home() {
        log.info("home page.");
        ModelAndView model = new ModelAndView();
        model.setViewName("home");
        return model;
    }
 
    @RequestMapping(value = "/")
    public ModelAndView homeBase() {
        log.info("base directory page.");
        ModelAndView model = new ModelAndView();
        model.setViewName("home");
        return model;
    }
 
}
问题描述:

访问根路径,后台控制器可以执行到:

?
1
log.info("base directory page.");
但是浏览器仅得到404的错误。
求大神帮忙看看,谢谢了。

展开
收起
a123456678 2016-03-17 15:40:37 2459 0
1 条回答
写回答
取消 提交回答
  • servlet-mapping 中的 /* 和 / 是有特殊含义的: 
    如果配置了 /* 那么会强制把所有请求的转给这个 servlet 处理,其他  servlet-mapping 均会被忽略; 
    而 / 表示默认 servlet,即在没有其他的 servlet 可以处理时,就被选中。 
    
    如果你使用的是 tomcat,你可以看一下 tomcat_base/conf/web.xml 
    里面有两个 servlet:default 和 jsp 
    
    因为你使用了 /*,所以当返回 jsp view 的时候,实际上是执行了 forward("home.jsp") 
    tomcat 在处理 home.jsp 的时候,因为使用了强制的 /* 映射, appServlet 被匹配到,
    而 controller 里匹配不到 home.jsp 的处理方法,自然就是 404 了 
    
    你可以这样更改你的配置: 
    <servlet-mapping> 
    <!-- 假如静态内容css和js放在 static 目录下,由容器定义的 default servlet 处理 --> 
    <servlet-name>default</servlet-name> 
    <url-pattern>/static/*</url-pattern> 
    </servlet-mapping> 
    
    <servlet-mapping> 
    <!-- 把  appServlet 当做默认 servlet --> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    2019-07-17 19:05:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多