springmvc整合路径404

qq_28132677 2016-12-28 03:58:42
先附上我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name>json_test</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 以下3项参数与log4j的配置相关 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- end -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring/spring-common.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- 配置spring -->
<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:conf/spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- 配置utf8-->
<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>
<!-- 由spring托管hibernate的持久层 -->
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


再就是spring配置


<?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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!--扫描映射 -->
<context:component-scan base-package="ssh"/>

<!-- 引入property配置文件 -->
<context:property-placeholder location="classpath:conf/prop/jdbc.properties"/>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.mysql.driverClassName}"></property>
<property name="url" value="${jdbc.mysql.url}"></property>
<property name="username" value="${jdbc.mysql.username}"></property>
<property name="password" value="${jdbc.mysql.password}"></property>
<!-- 初始化连接大小
<property name="initialSize" value="${jdbc.initialSize}"></property> -->
<!-- 连接池最大数量
<property name="maxActive" value="${jdbc.maxActive}"></property> -->
<!-- 连接池最大空闲
<property name="maxIdle" value="${jdbc.minIdle}"></property> -->
<!-- 连接池最小空闲
<property name="minIdle" value="${jdbc.minIdle}"></property> -->
<!-- 获取连接最大等待时间
<property name="maxWait" value="${jdbc.maxWait}"></property> -->
</bean>

<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.mysql.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!--是否显示sql语句 我在这里是显示的 -->
<prop key="hibernate.show_sql">${jdbc.show_sql}</prop>
<!--格式化显示sql语句 -->
<prop key="hibernate.format_sql">${jdbc.format_sql}</prop>
</props>
</property>
<!-- 自动扫描制定位置下的实体进行映射 -->
<property name="packagesToScan" value="ssh.entity"/>
</bean>

<!-- 配置一个事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 应该是开启事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>


最后就是spring mvc配置了


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

<!-- 注解扫描包 -->
<context:component-scan base-package="ssh" />

<!-- 开启注解 -->
<mvc:annotation-driven />

<!-- 定义视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="contentType" value="text/html" />
<property name="prefix" value="/"></property><!-- 视图文件的前缀 -->
<property name="suffix" value=".jsp"></property><!-- 视图文件的后缀名 -->
</bean>
</beans>

我的个人理解是在web.xml里面配置了
<url-pattern>/*</url-pattern> 

那么路径不管是加不加.do 或者加不加.jsp 应该都能匹配吧,现在启动的时候 默认访问的index.jsp不能访问,加了路径也不行
...全文
499 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mithiu 2016-12-28
  • 打赏
  • 举报
回复
我刚才把拦截路径改成/*试验了一下,没有报404的错......
Mithiu 2016-12-28
  • 打赏
  • 举报
回复
云晴 2016-12-28
  • 打赏
  • 举报
回复
引用 4 楼 shzy1988 的回复:
首先你的url没有写端口号,一般默认是8080。http://localhost:8080/mvc。然后确定你的web context-root是否是mvc(右击工程:properties--myeclipse-web--web Context-root)。一般这两个都正确的话,访问http://localhost:8080/mvc就能在页面上显示index.jsp的内容了
,出什么别的问题的话先看log报什么错
GrayHJX 2016-12-28
  • 打赏
  • 举报
回复
引用 10 楼 qq_28132677 的回复:
[quote=引用 8 楼 donggua3694857 的回复:] 你配了/*意味着你所有请求都会经过DispatcherServlet,你需要在配置文件里面加个<mvc:default-servlet-handler/>这样就能范围静态资源了
所有请求都会经过DispatcherServlet,我的理解是不是在web.xml里面配置的index.jsp ,DispatcherServlet会去搜索'index.jsp‘这个路径,当成类似index.jsp.do的样子, 然后找不到就404了? [/quote] 不是自动在后面加.do,由于没有通配符,它是会直接去找index.jsp。至于/*和/有什么不一样,老实说我也不知道配“/”到底拦了些什么。还望楼下有大神能指点一二
qq_28132677 2016-12-28
  • 打赏
  • 举报
回复
引用 11 楼 shzy1988 的回复:
你将web.xml里面的<url-pattern>/*</url-pattern>中的*去掉。<url-pattern>/</url-pattern>。这个你试试。/*和/是意思是不一样的
好使。。。但是我百度了说加*是包含不加的路径的,懵逼了
一个治疗术 2016-12-28
  • 打赏
  • 举报
回复
4楼说得对
双子叶 2016-12-28
  • 打赏
  • 举报
回复
你将web.xml里面的<url-pattern>/*</url-pattern>中的*去掉。<url-pattern>/</url-pattern>。这个你试试。/*和/是意思是不一样的
qq_28132677 2016-12-28
  • 打赏
  • 举报
回复
引用 8 楼 donggua3694857 的回复:
你配了/*意味着你所有请求都会经过DispatcherServlet,你需要在配置文件里面加个<mvc:default-servlet-handler/>这样就能范围静态资源了
所有请求都会经过DispatcherServlet,我的理解是不是在web.xml里面配置的index.jsp ,DispatcherServlet会去搜索'index.jsp‘这个路径,当成类似index.jsp.do的样子, 然后找不到就404了?
qq_28132677 2016-12-28
  • 打赏
  • 举报
回复
引用 8 楼 donggua3694857 的回复:
你配了/*意味着你所有请求都会经过DispatcherServlet,你需要在配置文件里面加个<mvc:default-servlet-handler/>这样就能范围静态资源了
访问index.jsp就是出现纯代码页面,不加Index.jsp就会404
GrayHJX 2016-12-28
  • 打赏
  • 举报
回复
你配了/*意味着你所有请求都会经过DispatcherServlet,你需要在配置文件里面加个<mvc:default-servlet-handler/>这样就能范围静态资源了
qq_28132677 2016-12-28
  • 打赏
  • 举报
回复
引用 3 楼 u010890358 的回复:
参考我博客吧
学习中,谢谢
zwbonline1112 2016-12-28
  • 打赏
  • 举报
回复
4#正解。。
qq_28132677 2016-12-28
  • 打赏
  • 举报
回复
引用 4 楼 shzy1988 的回复:
首先你的url没有写端口号,一般默认是8080。http://localhost:8080/mvc。然后确定你的web context-root是否是mvc(右击工程:properties--myeclipse-web--web Context-root)。一般这两个都正确的话,访问http://localhost:8080/mvc就能在页面上显示index.jsp的内容了
本地端口号已经被我改成80了,方便,另外确认context-root是mvc
双子叶 2016-12-28
  • 打赏
  • 举报
回复
首先你的url没有写端口号,一般默认是8080。http://localhost:8080/mvc。然后确定你的web context-root是否是mvc(右击工程:properties--myeclipse-web--web Context-root)。一般这两个都正确的话,访问http://localhost:8080/mvc就能在页面上显示index.jsp的内容了
青元子 2016-12-28
  • 打赏
  • 举报
回复
参考我博客吧
qq_28132677 2016-12-28
  • 打赏
  • 举报
回复
引用 1 楼 wlwlwlwl015 的回复:
先看一下tomcat下的logs目录的启动日志报什么异常,不一定是你springmvc的配置错误~有可能是其它问题
tomcat的logs 啥问题都没报
小灯光环 2016-12-28
  • 打赏
  • 举报
回复
先看一下tomcat下的logs目录的启动日志报什么异常,不一定是你springmvc的配置错误~有可能是其它问题

81,092

社区成员

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

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