码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC_Hello World

时间:2018-09-22 18:30:52      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:lan   page   coding   web app   ring   encoding   ref   source   ati   

【Hello World】

步骤:

(1)加入jar包,

(2)在web.xml中配置DispatcherServlet,

(3)加入Spring MVC的配置文件,

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

(5)编写视图

 

(1)

技术分享图片

 

(2)

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6 
 7     <!-- 配置DispatcherServlet -->
 8     <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
 9     <servlet>
10         <servlet-name>springDispatcherServlet</servlet-name>
11         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
12         <!-- 配置DispatcherServlet的一个初始化参数:配置Spring MVC配置文件的位置和名称 -->
13         <init-param>
14             <param-name>contextConfigLocation</param-name>
15             <param-value>classpath:springmvc.xml</param-value>
16         </init-param>
17         <load-on-startup>1</load-on-startup>
18     </servlet>
19 
20     <servlet-mapping>
21         <servlet-name>springDispatcherServlet</servlet-name>
22         <url-pattern>/</url-pattern>
23     </servlet-mapping>
24 </web-app>

 

(3)

springmvc.xml:

 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 9 
10    <!-- 配置自动扫描的包 -->
11    <context:component-scan base-package="com.hk.springmvc"></context:component-scan>
12    
13    <!-- 配置视图解析器:如何把handler方法返回值解析为物理视图 -->
14    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15         <property name="prefix" value="/WEB-INF/views/"></property>
16         <property name="suffix" value=".jsp"></property>
17    </bean>
18 </beans>

 

(4)

 1 package com.hk.springmvc.handlers;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 //控制器
 7 @Controller
 8 public class HelloWorld {
 9     
10     /*
11      * 1.使用@RequestMapping注解来映射请求的URL
12      * 2.返回值会通过视图解析器解析为物理的视图,对于InternalResourceViewResolver 视图解析器会做如下的解析:
13      *      prefix + returnval + 后缀,
14      *   通过这样的方式得到实际的物理视图,然后做转发操作
15      *   /WEB-INF/views/success.jsp
16      */
17     @RequestMapping("/helloworld")
18     public String hello(){
19         System.out.println("Hello World");
20         return "success";
21     }
22 
23 }

 

(5)

index.jsp:

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 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=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10    <a href="helloworld">Hello World</a>
11 </body>
12 </html>

 

success.jsp:

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 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=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10    <h4>SUCCESS</h4>
11 </body>
12 </html>

 

运行结果:

技术分享图片

 

点击“Hello World”

技术分享图片

 

附:

技术分享图片

 

Spring MVC_Hello World

标签:lan   page   coding   web app   ring   encoding   ref   source   ati   

原文地址:https://www.cnblogs.com/zhzcode/p/9690388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!