该文档主要记录SpringMVC-Maven 搭建及发布到Tomcat7操作流程
Round 1: 使用Eclipse创建Maven项目
创建名称SpringMVC-studyDemo的Maven项目
其中index.jsp报错,错误信息:Multiple annotations found at this line: - The superclass
意思是缺少servlet包,我们可以导入javax.servlet-api-3.1.0.jar包.
只需要在pom.xml中添加依赖即可
javax.servlet
javax.servlet-api
3.1.0
Round 2: 引入Spring MVC
这里主要是在pom.xml中增加对Spring的依赖
5.0.8.RELEASE
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
Round 3: web.xml 启动支持Spring MVC
这里主要是在web.xml中增加对Spring的启动配置
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">
Archetype Created Web Application
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springContext.xml
1
dispatcher
/
contextConfigLocation
classpath:springContext.xml
org.springframework.web.context.ContextLoaderListener
Round 4: 配置 springContext.xml
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
/WEB-INF/views/
.jsp
Round 5: 创建Spring控制器和视图
Controller 文件
package com.spring.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/helleworld")
public ModelAndView hellWorld() {
ModelAndView view=new ModelAndView("index.jsp");
return view;
}
}
jsp 文件
pageEncoding="ISO-8859-1"%>
Insert title herethis is helloworld jsp
Round 6: 编译项目发布部署
Maven install
在\study-demo\target\目录下生成study-demo.war 可直接部署到tomcat/webapp目录下。
Round 7: Q&A
待续.....