本文将对应用springmvc实现HelloWorld打印,其步骤如下:

1、加入相关jar包,在maven项目里就是在pom.xml文件中引入相关实际依赖

2、在web.xml文件中配置DispatcherServlet

3、加入springmvc配置文件

4、编写处理请求的请求处理器,并标识为处理器

5、编写视图

下面通过代码具体实现:

1、配置pom.xml文件,相当于加入jar包

HelloWorld实例(springmvc版)_dubbo

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4  5     <modelVersion>4.0.0</modelVersion> 6  7     <groupId>com.wn.third-ssmm</groupId> 8     <artifactId>third-ssmm</artifactId> 9     <version>1.0-SNAPSHOT</version>10     <packaging>war</packaging>11 12     <properties>13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>14         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>15         <spring.version>4.2.4.RELEASE</spring.version>16         <java.version>1.8</java.version><!-- 官方推荐 -->17     </properties>18 19     <!-- 引入实际依赖 -->20     <dependencies>21         <!-- spring -->22         <dependency>23             <groupId>org.springframework</groupId>24             <artifactId>spring-core</artifactId>25             <version>${spring.version}</version>26         </dependency>27         <dependency>28             <groupId>org.springframework</groupId>29             <artifactId>spring-beans</artifactId>30             <version>${spring.version}</version>31         </dependency>32         <dependency>33             <groupId>org.springframework</groupId>34             <artifactId>spring-context</artifactId>35             <version>${spring.version}</version>36         </dependency>37         <dependency>38             <groupId>org.springframework</groupId>39             <artifactId>spring-web</artifactId>40             <version>${spring.version}</version>41         </dependency>42         <dependency>43             <groupId>org.springframework</groupId>44             <artifactId>spring-webmvc</artifactId>45             <version>${spring.version}</version>46         </dependency>47         <dependency>48             <groupId>org.springframework</groupId>49             <artifactId>spring-jdbc</artifactId>50             <version>${spring.version}</version>51         </dependency>52         <!-- import lombok -->53         <!-- 作用:在源码中没有getter和setter方法,但是在编译生成的字节码文件中有getter和setter方法 -->54         <dependency>55             <groupId>org.projectlombok</groupId>56             <artifactId>lombok</artifactId>57             <version>1.16.8</version>58             <scope>provided</scope>59         </dependency>60     </dependencies>61 62     <build>63         <plugins>64             <plugin>65                 <groupId>org.apache.maven.plugins</groupId>66                 <artifactId>maven-compiler-plugin</artifactId>67                 <configuration>68                     <source>1.8</source>69                     <target>1.8</target>70                     <encoding>utf-8</encoding>71                 </configuration>72             </plugin>73         </plugins>74     </build>75 </project>

HelloWorld实例(springmvc版)_dubbo

2、在web.xml文件中配置DispatcherServlet

    DispatcherServlet的作用:如果在某个方法上配置了@RequestMapping("/helloworld"), 当浏览器访问helloworld时,DispatcherServlet会将这个请求发送给      @RequestMapping("/helloworld")所在的方法上,执行这个方法 

HelloWorld实例(springmvc版)_dubbo

 1 <?xml version="1.0" encoding="utf-8"?> 2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 5     <!-- 配置DispatcherServlet --> 6     <!-- 配置DispatcherServlet的作用是:如果在某个方法上配置了@RequestMapping("/helloworld"), 7          当浏览器访问helloworld时,DispatcherServlet会将这个请求发送给@RequestMapping("/helloworld") 8          所在的方法上,执行这个方法 --> 9     <servlet>10         <servlet-name>SpringDispatcherServlet</servlet-name>11         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>12         <!-- 配置DispatcherServlet的一个初始化参数:配置springMVC配置文件的位置和名称 -->13         <init-param>14             <param-name>contextConfigLocation</param-name>15             <param-value>classpath*:springmvc.xml</param-value>16         </init-param>17     <!--SpringDispatcherServlet在当前web应用被加载的时候被创建,而不是等第一次请求的时候被创建  -->18         <load-on-startup>1</load-on-startup>19     </servlet>20     <servlet-mapping>21         <servlet-name>SpringDispatcherServlet</servlet-name>22         <url-pattern>/</url-pattern>  <!-- 可以应答所有请求 -->23     </servlet-mapping>24     <welcome-file-list>25         <welcome-file>/index.jsp</welcome-file>26     </welcome-file-list>27 </web-app>

HelloWorld实例(springmvc版)_dubbo

3、配置springmvc.xml文件

HelloWorld实例(springmvc版)_dubbo

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  4     xmlns:context="http://www.springframework.org/schema/context" 5     xmlns:mvc="http://www.springframework.org/schema/mvc" 6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 8                            http://www.springframework.org/schema/context 
 9                            http://www.springframework.org/schema/context/spring-context-3.2.xsd10                            http://www.springframework.org/schema/mvc 
11                            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">12     <!-- 配置自动扫描的包 -->13     <context:component-scan base-package="com.atguigu.springmvc"/>14     <!-- 配置视图解析器:如何把handler方法返回值解析为实际视图 -->15     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">16         <property name="prefix" value="/WEB-INF/views/"/>17         <property name="suffix" value=".jsp"/>18     </bean>19 </beans>

HelloWorld实例(springmvc版)_dubbo

4、编写处理请求的请求处理器,并标识为处理器

HelloWorld实例(springmvc版)_dubbo

 1 package com.atguigu.springmvc.handlers; 2  3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5  6 @Controller //这样标识后HelloWorld成为控制器,在springmvc叫做handler处理器或请求处理器 7 public class HelloWorld { 8     /** 9      * 1、使用@RequestMapping注解来映射请求的URL10      * 2、返回值会通过视图解析器解析为实际的物理视图:对于InternalResourceViewResolver视图解析器,会做如下解析:11      *    通过prefix+returnvel+后缀这样的方式得到实际的物理视图,然后转发12      *    /WEB-INF/views/success.jsp13      * @return14      */15     @RequestMapping("/helloworld")16     public String hello() {17         System.out.println("HelloWorld");18         return "success";19     }20 }

HelloWorld实例(springmvc版)_dubbo

5、编写请求文件index.jsp

HelloWorld实例(springmvc版)_dubbo

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body>10     <!-- 超链接 -->11     <a href="/helloworld">HelloWorld</a>12 </body>13 </html>

HelloWorld实例(springmvc版)_dubbo

6、在WEB-IFN下新建views文件夹,在views文件夹下新建最终页面success.jsp,如下

HelloWorld实例(springmvc版)_dubbo

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body>10     <h4>Success Page</h4>11 </body>12 </html>

HelloWorld实例(springmvc版)_dubbo

运行结果:

HelloWorld实例(springmvc版)_mybatis_13

注:1、并不一定严格按照上述创建各个文件的先后顺序完善各个文件内容,各个文件交叉配置

      2、在web.xml文件中,我们也可以不适用contextConfigLocation来配置springmvc配置文件的位置和名称,可以使用默认的,

      默认的配置文件为:/WEB-IFN/<servlet-name>-servlet.xml ,即将springmvc.xml文件移至WEB-IFN文件夹下,并该名为

     SpringDispatcherServlet-servlet.xml。

      3、有关spring mvc访问静态资源的问题

  关于使用spring mvc处理静态资源,比如html(发现之前的springmvc.xml中<property name = "suffix" value = ".jsp"></property>定义为jsp结尾就可以成功跳转,但是如果改为html并在web-inf下面新建了html文件后,并将suffix这里的".jsp"改为".html",无法跳转到想要的html页面,并且给出404错误,同时console给出错误信息为:No mapping found for HTTP request with URI [/springTest/WEB-INF/views/result.html] in DispatcherServ)

     原因是:该 servlet 对应的 url-pattern 定义成 /,因此该 servlet 会匹配上诸如 /p_w_picpaths/a.jpg, /css/hello.css 等这些静态资源,甚至包括 /jsp/stock/index.jsp 这些 jsp 也会匹配。但是并没有定义相应的 Controller 来处理这些资源,因此这些请求通常是无法完成的。 

     所以,在web.xml中加入

HelloWorld实例(springmvc版)_dubbo

 1 <servlet-mapping> 2      <servlet-name>default</servlet-name> 3      <url-pattern>*.css</url-pattern> 4 </servlet-mapping> 5  6 <servlet-mapping> 7       <servlet-name>default</servlet-name> 8       <url-pattern>*.gif</url-pattern> 9 </servlet-mapping>10 11 <servlet-mapping>12     <servlet-name>default</servlet-name>13      <url-pattern>*.jpg</url-pattern>14 </servlet-mapping>15 16 <servlet-mapping>17      <servlet-name>default</servlet-name>18      <url-pattern>*.js</url-pattern>19 </servlet-mapping>20 21 <servlet-mapping>22       <servlet-name>default</servlet-name>23       <url-pattern>*.html</url-pattern>24 </servlet-mapping>

HelloWorld实例(springmvc版)_dubbo

将springmvc.xml改为:

1 class="org.springframework.web.servlet.view.InternalResourceViewResolver">2             <property name = "prefix" value="/WEB-INF/views/"></property>3             <property name = "suffix" value = ".jpg"></property>4 </bean>5

就可以访问静态资源.jpg格式的图片了。