J2EE搭建Dynamic web SpringMVC工程404错误分析(一)
2017-05-07 15:57
423 查看
目录结构如下:
![](https://img-blog.csdn.net/20170507153525845?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHhsbXljc2RuZnJlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
自己写的web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:HelloWeb-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Test</welcome-file>
</welcome-file-list>
</web-app>
是指定自己的Spring配置的xml.
问题:
对于不指定的条件下,默认是就是[serveletname]-servlet.xml默认路径是webcontent文件夹下面和web.xml同级就可以。但是有的时候会爆出不能找到[servletname]-servlet.xml;那么把该xml文件移到src文件夹下面就可以了。---- 这个问题暂时还没有研究。
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:HelloWeb-servlet.xml</param-value>
</init-param>
这个是配置请求文件的跳转,也就是当有请求的时候直接跳转Test,因为你的spring控制器配置如下:
![](https://img-blog.csdn.net/20170507154540490?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHhsbXljc2RuZnJlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
MainController.ava代码:
package web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
//@RequestMapping("/Web")
public class MainController {
@RequestMapping(value="/Test", method=RequestMethod.GET)
public String returnJSP(){
return "MyWeb"; //JSP 名字
}
}
控制器会将请求跳转到Test,这也是问什么配置:
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:HelloWeb-servlet.xml</param-value>
</init-param>同时还需要添加一个Test空文件在webcontent下:
![](https://img-blog.csdn.net/20170507154857396?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHhsbXljc2RuZnJlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
不添加会爆出404错误!!!!!就是自己制定请求的url ---> http://localhost:8080/twoDynamic/Web/Test
但是添加了空的Test文件:
![](https://img-blog.csdn.net/20170507155056961?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHhsbXljc2RuZnJlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
不用使用全的url便可以请求。但是反而输入全的url爆出了404:
![](https://img-blog.csdn.net/20170507155242885?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHhsbXljc2RuZnJlZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
这个原因可能:在spring控制器中已经添加了跳转,所以没有必要请输入全url。
HelloWeb-servlet.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/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="web.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
自己写的web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:HelloWeb-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Test</welcome-file>
</welcome-file-list>
</web-app>
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:HelloWeb-servlet.xml</param-value> </init-param>
是指定自己的Spring配置的xml.
问题:
对于不指定的条件下,默认是就是[serveletname]-servlet.xml默认路径是webcontent文件夹下面和web.xml同级就可以。但是有的时候会爆出不能找到[servletname]-servlet.xml;那么把该xml文件移到src文件夹下面就可以了。---- 这个问题暂时还没有研究。
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:HelloWeb-servlet.xml</param-value>
</init-param>
这个是配置请求文件的跳转,也就是当有请求的时候直接跳转Test,因为你的spring控制器配置如下:
MainController.ava代码:
package web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
//@RequestMapping("/Web")
public class MainController {
@RequestMapping(value="/Test", method=RequestMethod.GET)
public String returnJSP(){
return "MyWeb"; //JSP 名字
}
}
控制器会将请求跳转到Test,这也是问什么配置:
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:HelloWeb-servlet.xml</param-value>
</init-param>同时还需要添加一个Test空文件在webcontent下:
不添加会爆出404错误!!!!!就是自己制定请求的url ---> http://localhost:8080/twoDynamic/Web/Test
但是添加了空的Test文件:
不用使用全的url便可以请求。但是反而输入全的url爆出了404:
这个原因可能:在spring控制器中已经添加了跳转,所以没有必要请输入全url。
HelloWeb-servlet.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/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="web.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
相关文章推荐
- J2EE搭建Dynamic web SpringMVC工程404错误分析(二)
- J2EE搭建Dynamic web SpringMVC工程404错误分析(三)
- SpringMVC+Spring搭建web工程环境
- J2EE dynamic web工程搭建 struts2
- 简单粗暴JavaWeb(基于SpringMvc+Maven+Mysql)-第一篇:web工程搭建
- eclipse搭建maven web 工程,和集成springmvc+mybatis的一些问题
- web框架之Spring-MVC环境搭建
- Spring-MVC初步-搭建一个web程序
- 搭建Dynamic Web Project(动态web项目)的springmvc工程2
- maven+spring mvc环境搭建xml版(无web.xml,maven jetty插件运行)
- Spring Web MVC项目搭建(一)
- maven+springMVC+hibernate工程搭建(四简单base层实现及demo)
- 使用IDEA基于Maven搭建多模块聚合工程(springmvc+spring+mybatis整合)
- Spring Web MVC 入门分析
- Spring 源码分析(四) ——MVC(一)Web 基础
- 从零开始搭建maven+springmvc+mybatis web项目(2)---springmvc配置文件编写
- j2ee 简单网站搭建:(二)添加和配置 spring + spring-mvc 的 mvc 开发环境
- spring SpringMVC MyBatis 搭建项目 MavenWeb项目
- web框架之Spring-MVC环境搭建
- IDEA上面搭建一个SpringBoot的web-mvc项目