SpringMVC框架

SpringMVC是一种基于Java实现的Web框架,它根据MVC思想将web进行职责解耦。

Componet

spring mvc包括以下组件,

1
2
3
4
Dispatcher Servlet - 前端控制器
Handler Mapping - 处理器映射器
Controller - 页面控制器
View Resolver - 试图解析器

How to use in eclipse

需要提前安装:

1
2
3
4
java
eclipse
tomcat
maven

new dynamic web project

新建动态web工程:

1
File -> other -> web -> dynamic web project

设置:
项目名称、服务器版本

maven project

转换为maven工程:

1
properties -> configure -> convert to maven project

add dependencies

利用maven来管理依赖,

1
2
3
4
5
6
7
8
9
10
11
12
pom.xml -> dependencies
add ->

groupId - artifactId - version
------------
org.springframework - spring-context - 4.2.5.RELEASE
org.springframework - spring-aop - 4.2.5.RELEASE
org.springframework - spring-webmvc - 4.2.5.RELEASE
org.springframework - spring-web - 4.2.5.RELEASE
javax.servlet - jstl - 1.2
commons-logging - commons-logging - 1.1.3
------------

add hello-servlet.xml

<context:componet-scan>用来设置需要加载的控制类所在的包,
<bean id="viewResolver" ... >用来解析view并自动添加前缀和后缀

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.hello" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

add web.xml

<servlet-name>设置servlet.xml文件的名称,如下对应的应该为hello-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

add controller class

添加包com.hello.controller以及Hello控制类,定义映射关系welcome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.hello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Hello {
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "Hello";
return new ModelAndView("welcome", "message", message);
}
}

其中,ModelAndView对象会解析view并把结果返回给请求端,message是传递给view的值。

那么,根据hello-servlet.xml中的设置,welcome会自动补全为/WEB-INF/jsp/welcome.jsp

add view

添加默认页面index.jsp,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<html>
<head>
<title>Spring MVC Tutorial Series by Crunchify.com</title>
<style type="text/css">
body {
background-image: url('http://crunchify.com/bg.png');
}
</style>
</head>
<body>
<br>
<div style="text-align:center">
<h2>
This is your Spring MCV<br>
</h2>
<h3>
<a href="welcome.html">Click here to See Welcome Message... </a>
</h3>
</div>
</body>
</html>

添加welcome.jsp

1
<h1>${message}</h1>

maven build

maven编译,在target目录产生编译结果

1
2
3
4
5
properties -> run as -> maven build ... 
Add Goals:
----
clean install
----