spring mvc 返回404error

wangyunzhong 发布于 2014/12/01 14:02
阅读 2K+
收藏 0

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

使用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;
	}

}

问题描述:

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

log.info("base directory page.");
但是浏览器仅得到404的错误。

求大神帮忙看看,谢谢了。

加载中
0
南湖船老大
南湖船老大
不推荐这种写法,我更喜欢写成 *.do 这种,也不需要考虑静态资源了
wangyunzhong
wangyunzhong
受教了。刚接触web相关,谢谢指点。
0
JeffreyLin
JeffreyLin
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>


wangyunzhong
wangyunzhong
知其然,知其所以然,给32个赞!
0
IDEA插件FastRequest
IDEA插件FastRequest
web.xml第17行改成/试试看
wangyunzhong
wangyunzhong
恩,确实是这个问题。谢谢了。
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部