该文档主要记录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中添加依赖即可
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!--有关jar包 依赖 可登录http://mvnrepository.com/ 查询对应jar包-->
</dependency>
Round 2: 引入Spring MVC
这里主要是在pom.xml中增加对Spring的依赖
<properties>
<spring.version>5.0.8.RELEASE</spring.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
Round 3: web.xml 启动支持Spring MVC
这里主要是在web.xml中增加对Spring的启动配置
<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>Archetype Created Web Application</display-name>
<servlet>
<!--DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派-->
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--servlet 加载配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--全局加载 配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--此处可以配置多项-->
<!--classpath默认为 src/main/resources-->
<param-value>classpath:springContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Round 4: 配置 springContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 搜索spring控件 -->
<!-- 这里 base-package 地址为 src/main/java 下建立 默认 controller的包名-->
<context:component-scan base-package="com.spring.mvc.controller"></context:component-scan>
<!-- 视图页面配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
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 文件
<%@ 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>Insert title here</title>
</head>
<body>
<p>this is helloworld jsp</p>
</body>
</html>
Round 6: 编译项目发布部署
Maven install
在\study-demo\target\目录下生成study-demo.war 可直接部署到tomcat/webapp目录下。
Round 7: Q&A
待续.....
来源:51CTO
作者:洛山红茶
链接:https://blog.51cto.com/85608547/2161548