笔记14 Hello Spring MVC

xiaoxiao2021-03-01  5

搭建一个SpringMVC所需要的包:

1.创建MySpringMVC项目,使用dynamic web project的方式。

2.导入jar包,放到/WebContent(WebRoot)/WEB-INF/lib目录下

3.关联jar包,Build Path

4.在WEB-INF目录下创建 web.xml配置Spring MVC的入口 DispatcherServlet,把所有的请求都提交到该Servlet注意:<servlet-name>myspringmvc</servlet-name>myspringmvc这个名字在下一步会用到

1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 <servlet> 7 <servlet-name>myspringmvc</servlet-name> 8 <servlet-class> 9 org.springframework.web.servlet.DispatcherServlet 10 </servlet-class> 11 <load-on-startup>1</load-on-startup> 12 </servlet> 13 <servlet-mapping> 14 <servlet-name>myspringmvc</servlet-name> 15 <url-pattern>/</url-pattern> 16 </servlet-mapping> 17 </web-app>

5.在WEB-INF目录下创建myspringmvc-servlet.xml,myspringmvc-servlet.xml 与上一步中的<servlet-name>myspringmvc</servlet-name>对应。

这是Spring MVC的 映射配置文件表示访问路径/index会交给id=indexController的bean处理id=indexController的bean配置为类:IndexController

1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 3 <beans> 4 <bean id="simpleUrlHandlerMapping" 5 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 6 <property name="mappings"> 7 <props> 8 <prop key="/index">indexController</prop> 9 </props> 10 </property> 11 </bean> 12 <bean id="indexController" class="controller.IndexController"></bean> 13 </beans>

6.控制类 IndexController

控制类 IndexController实现接口Controller ,提供方法handleRequest处理请求SpringMVC通过 ModelAndView 对象把模型和视图结合在一起

ModelAndView mav = new ModelAndView("index.jsp"); mav.addObject("message", "Hello Spring MVC");

表示视图是index.jsp模型数据是 message,内容是 “Hello Spring MVC”

1 package controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.Controller; 8 9 public class IndexController implements Controller { 10 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 11 ModelAndView mav = new ModelAndView("index.jsp"); 12 mav.addObject("message", "Hello Spring MVC"); 13 return mav; 14 } 15 }

7.视图index.jsp

1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index2.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <h1>${message}</h1> 27 </body> 28 </html>

8.运行

原理图:

1. 用户访问 /index2. 根据web.xml中的配置 所有的访问都会经过DispatcherServlet3. 根据 根据配置文件springmvc-servlet.xml ,访问路径/index会进入IndexController类4. 在IndexController中指定跳转到页面index.jsp,并传递message数据5. 在index.jsp中显示message信息

 

 

转载请注明原文地址: https://www.6miu.com/read-3450331.html

最新回复(0)