最开始一直认为是自己设置的有问题,参照了无数网址经验后,发现是jar包问题。之前使用的是spring3.0.5就是不行,使用spring4.1.6一切嗷嗷地正常!
1 环境搭建:
参照:问题:137. spring--springweb mvc4.1.6环境搭建
2 项目地址:
F:\Tutorial\Java\Spring\SpringMVCTutorial
https://win-9ris1mc6f8k/svn/SmartCode/SpringCruise/SpringMVCTutorial-4.1.6
3 项目结构:
项目依赖jar包
4 关键点:
1、web.xml
xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc-servlet.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
2、springmvc-servlet.xml
xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scanbase-package="test.SpringMVC"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<mvc:resourcesmapping="/pages/**"location="/WEB-INF/pages/"/>
<mvc:resourcesmapping="/Pictures/**"location="/WEB-INF/Pictures/"/>
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<propertyname="prefix"value="/WEB-INF/jsp/"/>
<propertyname="suffix"value=".jsp"/>
bean>
beans>
3、mvcController.java
packagetest.SpringMVC;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/mvc")
publicclassmvcController {
@RequestMapping("/hello")
public String hello(){
return"hello";
}
@RequestMapping("/staticPage")//value = "/staticPage", method = RequestMethod.GET)
public String redirect(){
return"redirect:/pages/final.htm";
}
@RequestMapping("/staticPictures")//value = "/staticPage", method =RequestMethod.GET)
public StringredirectPictures() {
return"redirect:/Pictures/Jackie.jpg";
}
}
4、hello.jsp
<%@ pagelanguage="java"contentType="text/html;charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
DOCTYPEhtmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
Hello World SPRING!
body>
html>
5、final.htm
<html>
<head>
<title>Spring Static Pagetitle>
head>
<body>
<h2>A simple HTML pageh2>
body>
html>
138.5 测试网址:
未配置正常效果:
description The requested resource is not available.
HTTP Status 404 - /SpringMVCTutorial/Pictures/Jackie.jpg
http://localhost:8060/SpringMVCTutorial/Pictures/Jackie.jpg
正常后效果:
http://localhost:8060/SpringMVCTutorial/mvc/hello