关于Spring MVC配置的问题--Spring MVC配置不生效

me阿木 2014-09-12 03:46:11
练习搭建SpringMVC项目,配置完成后发现无法实现路径拦截。
先给出配置:
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>
<display-name>bursar</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-hibernate.xml</param-value>
<description>Hibernate config</description>
</context-param>

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

<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>


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

<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="net.ibuluo.bursar" />

<!-- 默认注解样式支持 -->
<mvc:annotation-driven />

<!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL-->
<mvc:default-servlet-handler/>

<!-- 定义JSP文件的位置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views"/>
<property name="suffix" value=".jsp"/>
</bean>

<!-- 定义无Controller的path<->view直接映射
<mvc:view-controller path="/" view-name="redirect:/management/index"/>
-->
</beans>


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

<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/bursar" />
<property name="username" value="root" />
<property name="password" value="zhangrui" />
</bean>

<!-- 配置hibernate SessionFactory-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
</props>
</property>
</bean>
</beans>


只有一个Controller文件:

package net.ibuluo.bursar.web;

import java.util.Map;

import javax.annotation.Resource;

import net.ibuluo.bursar.entity.main.User;
import net.ibuluo.bursar.service.UserService;

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

@Controller
@RequestMapping(value="/user")
public class UserController {


private final String TEST = "/user/test";

@Resource
private UserService userService;

@RequestMapping(method = RequestMethod.GET)
public String load() {System.out.println("123456");
return TEST;
}

@RequestMapping(value="/test", method=RequestMethod.GET)
public String test(Map<String, Object> map){
User user = userService.get(1);
map.put("user", user);
return TEST;
}

}


要访问的页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>test</title>
</head>
<body>
<h1>Just A Test!</h1>
</body>
</html>


整体项目结构:


启动过程没有提示任何错误。
访问主页:


错误信息:
访问主页,url未作处理,可依然能访问:


访问目标页面,使用路径http://localhost:8080/bursar-web/user/test,结果如下:


使用原始路径可以正常访问:
...全文
658 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
姜小白- 2014-09-13
  • 打赏
  • 举报
回复
private final String TEST = "/user/test"; 改为 private final String TEST = "user/test"; <property name="prefix" value="/views"/> 改为 <property name="prefix" value="/views/" />
三流角色 2014-09-13
  • 打赏
  • 举报
回复
<property name="prefix" value="/views"/> 换成这样试试 <property name="prefix" value="/views/"/>
Golden_Dog 2014-09-12
  • 打赏
  • 举报
回复
加views呢?http://localhost:8080/bursar-web/views/user/test

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧