天天看點

Spring MVC搭建(一)需要的包:Spring MVC流程目錄結構配置web.xml

需要的包:

spring-beans-4.1.2.jar

spring-context-4.1.2.jar

spring-web-3.2.8.jar

spring-webmvc-3.2.8.jar

Spring MVC流程

1.用戶端發送請求- - -DispatcherServlet控制器收到請求- - - 交給HandlerMapping處理映射器

2.HandlerMapping處理映射器根據請求路徑(URL)找到相應的HandlerAdapter處理擴充卡(Controller)

3.HandlerAdapter處理一些功能請求,傳回ModelAndView對象

4.ViewResolver視圖解析器,将ModelAndView解析成具體視圖

5.将model模型中的資料渲染到view中傳回給使用者

目錄結構

Spring MVC搭建(一)需要的包:Spring MVC流程目錄結構配置web.xml

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" id="WebApp_ID" version="2.5">
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-mvc.xml
        </param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
           

spring-mvc.xml配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <!--掃描包,如果不寫控制類的注解将不會起作用-->
    <context:component-scan base-package="top.springmvc"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
           

IndexController.java

package top.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
    @RequestMapping("/hello")
    public String getHtml(Model model){
        model.addAttribute("message", "HELLO");
        return "hello";
    }
}
           

model.addAttribute(“message”, “HELLO”),将模型的message的值設為HELLO

hello.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>${message} </h1>
</body>
</html>
           

index,jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1><a href="hello.do">點選跳轉</a></h1>
</body>
</html>
           

繼續閱讀