关于spring-mvc:你知道目前最流行的SpringMVC框架如何搭建吗

24次阅读

共计 6645 个字符,预计需要花费 17 分钟才能阅读完成。

Spring MVC 是 Spring 家族中的一个 web 成员, 它是一种基于 Java 的实现了 Web MVC 设计思维的申请驱动类型的轻量级 Web 框架,即 应用了 MVC 架构 模式的思维,将 web 层进行职责解耦,基于申请驱动指的就是应用申请 - 响应模型,框架的目标就是帮忙咱们简化开发,Spring MVC 也是要简化咱们日常 Web 开发的。

Spring MVC 是服务到工作者思维的实现。前端控制器是 DispatcherServlet;利用控制器拆为处理器映射器 (Handler Mapping) 进行处理器治理和视图解析器 (View Resolver) 进行视图治理;反对本地化 / 国际化(Locale)解析及文件上传等;提供了非常灵活的数据验证、格式化和数据绑定机制;提供了弱小的约定大于配置(常规优先准则)的契约式编程反对。

SpringMVC 搭建的形式

  1. 开发环境搭建
  2. 新建 Maven webApp
  3. Springmvc 环境 jar 包依赖
  4. 配置 web.xml (前端控制器配置)
  5. servlet-context.xml 配置
  6. 页面控制器的编写
  7. 增加视图页面
  8. 启动 jetty 服务器

案例实操

开发环境搭建

Eclipse + jdk1.7 + maven + Jetty

新建 Maven webApp

建设 springmvc01 工程并调整 web 环境。

Springmvc 环境 jar 包依赖

<project xmlns=”http://maven.apache.org/POM/4.0.0″
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven…d”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>springmvc01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!– spring web –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!– spring mvc –>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!– web servlet –>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<!– jetty 插件 –>
<build>
<finalName>springmvc01</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<!– 编译环境插件 –>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<contextPath>/springmvc01</contextPath>
</configuration>
</plugin>
</plugins>
</build>
</project>

配置 web.xml (前端控制器配置)

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app id=”WebApp_ID” 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/ja…d”>

<!– 示意容器启动时 加载上下文配置 这里指定 spring 相干配置 –>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:*.xml</param-value>

</context-param>


<!– 启用 spring 容器环境上下文监听 –>

<listener>

<listener class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!– 编码过滤 utf-8 –>

<filter>

<description>char encoding filter</description>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!– servlet 申请散发器 –>

<servlet>

<servlet-name>springMvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:servlet-context.xml</param-value>

</init-param>

<!– 示意启动容器时初始化该 Servlet –>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springMvc</servlet-name>

<!– 这是拦挡申请, / 代表拦挡所有申请, 拦挡所有.do 申请 –>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

要想启动咱们的 springMvc 环境,目前对于 mvc 框架的配置还未进行。以上在 web.xml 中援用了 servlet-context.xml 文件。

servlet-context.xml 配置

<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
xsi:schemaLocation=”
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…
http://www.springframework.or…d”>

<!– 扫描 com.xxx.controller 下包 –>

<context:component-scan base-package=”com.xxx.controller” />

<!– mvc 申请映射 处理器与适配器配置 –>

<mvc:annotation-driven/>

<!– 配置视图解析器 默认的视图解析器 - –>

<bean id=”defaultViewResolver”

class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>

<property name=”viewClass”

value=”org.springframework.web.servlet.view.JstlView” />

<property name=”contentType” value=”text/html” />

<property name=”prefix” value=”/WEB-INF/jsp/” />

<property name=”suffix” value=”.jsp” />

</bean>

</beans>

如果返回乱码:配置音讯转换器

<!– 音讯转换器 –>

<mvc:message-converters register-defaults=”true”>

<bean class=”org.springframework.http.converter.StringHttpMessageConverter”>

<property name=”supportedMediaTypes” value=”text/html;charset=UTF-8″/>

</bean>

</mvc:message-converters>

页面控制器的编写

/**

  • 采纳注解扫描模式


*/

@Controller

public class HelloController {

/**

  • 申请映射地址 /hello

  • @return


*/

@RequestMapping(“/hello”)

public ModelAndView hello(){

ModelAndView mv=new ModelAndView();

mv.addObject(“hello”, “hello spring mvc”);

mv.setViewName(“hello”);

return mv;

}

}

增加视图页面

在 WEB-INF 下新建 jsp 文件夹,并在文件加下新建 hello.jsp

<%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%>

<%
String path = request.getContextPath();
String basePath =
request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>

<html>

<head>

<base href=”<%=basePath%>”>

<title>My JSP ‘hello.jsp’ starting page</title>

<meta http-equiv=”pragma” content=”no-cache”>

<meta http-equiv=”cache-control” content=”no-cache”>

<meta http-equiv=”expires” content=”0″>

<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>

<meta http-equiv=”description” content=”This is my page”>

</head>

<body>

<!– el 表达式接管参数值 –>

${hello}

</body>

</html>

启动 jetty 服务器

选中我的项目右键 → run as → maven build → goals 输入框中输出 jetty:run 即可启动服务器。

如果这里启动胜利,浏览器(最好用弱小的 chrome 或者火狐浏览器,程序员的最爱!!!)拜访地址 http://localhost:8080/springmvc01/hello

最终成果如下:

至此,springmvc 环境搭建结束。

扩大

Spring MVC 能帮咱们做什么

  1. 让咱们能非常简单的设计出洁净的 Web 层;
  2. 进行更简洁的 Web 层的开发;
  3. 天生与 Spring 框架集成(如 IoC 容器、AOP 等);
  4. 提供弱小的约定大于配置的契约式编程反对;
  5. 能简略的进行 Web 层的单元测试;
  6. 反对灵便的 URL 到页面控制器的映射;
  7. 非常容易与其余视图技术集成,如 Velocity、FreeMarker 等等,因为模型 数据不放在特定的 API 里,而是放在一个 Model 里(Map 数据结构实现,因而很容易被其余框架应用);
  8. 非常灵活的数据验证、格式化和数据绑定机制,能应用任何对象进行数据绑定,不用实现特定框架的 API;
  9. 反对灵便的本地化等解析;
  10. 更加简略的异样解决;
  11. 对动态资源的反对;
  12. 反对 Restful 格调。
正文完
 0