Spring MVC_Hello World

2021-06-19 11:05

阅读:305

YPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

标签: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     
 8     
 9     servlet>
10         servlet-name>springDispatcherServletservlet-name>
11         servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
12         
13         init-param>
14             param-name>contextConfigLocationparam-name>
15             param-value>classpath:springmvc.xmlparam-value>
16         init-param>
17         load-on-startup>1load-on-startup>
18     servlet>
19 
20     servlet-mapping>
21         servlet-name>springDispatcherServletservlet-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    
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  2     pageEncoding="ISO-8859-1"%>
 3 
 4 
 5 6
 7 Insert title here 8 
 9 
10    Hello World
11 
12 

 

success.jsp:

 1  2     pageEncoding="ISO-8859-1"%>
 3 
 4 
 5 
 6  7 Insert title here 8 
 9 
10    

SUCCESS

11 12

 

运行结果:

技术分享图片

 

点击“Hello World”

技术分享图片

 

附:

技术分享图片

 

Spring MVC_Hello World

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

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


评论


亲,登录后才可以留言!