SpringMVC的Hello World

本次使用Maven和Spring IO platform创建SpringMVC的Hello World。

一、Maven的Pom文件内容如下:

 1 <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-v4_0_0.xsd">
 2     <modelVersion>4.0.0</modelVersion>
 3     <groupId>com.mcs.springmvc</groupId>
 4     <artifactId>HelloWorld</artifactId>
 5     <packaging>war</packaging>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <name>HelloWorld SpringMVC</name>
 8     <url>http://maven.apache.org</url>
 9 
10     <dependencies>
11         <!-- junit -->
12         <dependency>
13             <groupId>junit</groupId>
14             <artifactId>junit</artifactId>
15             <scope>test</scope>
16         </dependency>
17         <!-- log4j -->
18         <dependency>
19             <groupId>log4j</groupId>
20             <artifactId>log4j</artifactId>
21         </dependency>
22 
23         <!-- spring -->
24         <dependency>
25             <groupId>org.springframework</groupId>
26             <artifactId>spring-webmvc</artifactId>
27         </dependency>
28         
29         <!-- commons -->
30         <dependency>
31             <groupId>commons-logging</groupId>
32             <artifactId>commons-logging</artifactId>
33         </dependency>
34 
35     </dependencies>
36 
37     <dependencyManagement>
38         <dependencies>
39             <dependency>
40                 <groupId>io.spring.platform</groupId>
41                 <artifactId>platform-bom</artifactId>
42                 <version>1.1.4.RELEASE</version>
43                 <type>pom</type>
44                 <scope>import</scope>
45             </dependency>
46         </dependencies>
47     </dependencyManagement>
48 
49     <build>
50         <finalName>HelloWorld</finalName>
51     </build>
52 </project>
View Code

二、Web.xml文件内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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" id="WebApp_ID" version="3.0">
 3     <display-name>springmvc</display-name>
 4     
 5     <!-- 配置SpringMVC -->
 6     <servlet>
 7         <servlet-name>springDispatcherServlet</servlet-name>
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9         <init-param>
10             <param-name>contextConfigLocation</param-name>
11             <param-value>classpath:spring-mvc.xml</param-value>
12         </init-param>
13         <load-on-startup>1</load-on-startup>
14     </servlet>
15 
16     <servlet-mapping>
17         <servlet-name>springDispatcherServlet</servlet-name>
18         <url-pattern>/</url-pattern>
19     </servlet-mapping>
20 
21     <!-- 配置字符集过滤器 -->
22     <filter>
23         <filter-name>characterEncodingFilter</filter-name>
24         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
25         <init-param>
26             <param-name>encoding</param-name>
27             <param-value>utf-8</param-value>
28         </init-param>
29     </filter>
30     <filter-mapping>
31         <filter-name>characterEncodingFilter</filter-name>
32         <url-pattern>/*</url-pattern>
33     </filter-mapping>
34 
35     <!-- 配置session超时时间,单位分钟 -->
36     <session-config>
37         <session-timeout>15</session-timeout>
38     </session-config>
39 
40     <!-- 设置欢迎页面 -->
41     <welcome-file-list>
42         <welcome-file>/index.jsp</welcome-file>
43     </welcome-file-list>
44 
45     <!-- 找不到页错误转向 -->
46     <error-page>
47         <error-code>404</error-code>
48         <location>/error/404.jsp</location>
49     </error-page>
50     <!-- 系统内部错误转向 -->
51     <error-page>
52         <error-code>500</error-code>
53         <location>/error/500.jsp</location>
54     </error-page>
55 </web-app>
View Code

三、spring-mvc文件内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 3         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
 4         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
 5 
 6     <!-- 使用注解的包 -->
 7     <context:component-scan base-package="com.mcs.springmvc.controller"></context:component-scan>
 8 
 9     <!-- 支持对象与JSON相互转换 -->
10     <mvc:annotation-driven />
11 
12     <!-- 视图解析器 -->
13     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14         <property name="prefix" value="/WEB-INF/views/" />
15         <property name="suffix" value=".jsp" />
16     </bean>
17 
18 
19 </beans>
View Code

四、HelloworldController文件内容如下:

 1 package com.mcs.springmvc.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.ui.Model;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 @Controller
 8 public class HelloworldController {
 9     
10     @RequestMapping("/helloWorld")
11     public String helloWorld(Model model) {
12         model.addAttribute("message", "Hello SpirngMVC!");
13         return "helloWorld";
14     }
15 }
View Code

五、helloworld.jsp文件内容如下:

 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     <h3>Message: ${ message }</h3>
11 </body>
12 </html>
View Code

六、显示效果如下:  

  Message: Hello SpirngMVC!

 

posted @ 2015-11-06 09:17  小个子猫  阅读(243)  评论(0编辑  收藏  举报