Spring MVC(二)基於標註的MVC
阿新 • • 發佈:2018-11-10
1.基於標註的Spring MVC
1.1 建立一個專案匯入jar包(ioc aop mvc) 拷貝容器對應的配置檔案到src下
在WEB-INF建立一個login.jsp
1.2 在web.xml 配置一個DispatcherServlet 並且使用
初始化引數 contextConfigLocation 關聯容器對應的配置檔案
1.3 開啟基於標註 Spring MVC
開啟元件掃描 <context:component-scan base-package="" />
開啟mvc的標註 <mvc:annotation-driven /> 自動配置了一個HandlerMapping
1.4 寫一個普通的java類使用@Controller 把java類變成控制器
控制器方法的返回值可以是String也可以是 ModelAndView
方法名任意引數任意
在控制器方法上加 @RequestMapping("/請求路徑")
1.5 配置檢視處理器
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:lang="http://www.springframework.org/schema/lang" 6mvc.xmlxmlns:mvc="http://www.springframework.org/schema/mvc" 7 xmlns:util="http://www.springframework.org/schema/util" 8 xmlns:task="http://www.springframework.org/schema/task" 9 xmlns:aop="http://www.springframework.org/schema/aop" 10 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 11http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd 12 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 14 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 16 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 17 <!-- 開啟元件掃描 --> 18 <context:component-scan base-package="com.xcz"></context:component-scan> 19 <!-- 開啟mvc標註 --> 20 <mvc:annotation-driven></mvc:annotation-driven> 21 <!-- 配置檢視處理器 --> 22 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 23 <property name="prefix" value="/WEB-INF/"></property> 24 <property name="suffix" value=".jsp"></property> 25 </bean> 26 </beans>
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 <form action="" method="post"> 11 賬號:<input type="text" value="請輸入賬號..."><br> 12 密碼:<input type="text" value="請輸入密碼..."><br> 13 <input type="submit" value="登入"><br> 14 </form> 15 </body> 16 </html>login.jsp
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 3 <display-name>SpringMVC-03</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <!-- 配置請求入口 --> 13 <servlet> 14 <servlet-name>SpringMVC</servlet-name> 15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 16 <!-- 配置初始化引數 --> 17 <init-param> 18 <param-name>contextConfigLocation</param-name> 19 <param-value>classpath:mvc.xml</param-value> 20 </init-param> 21 <load-on-startup>1</load-on-startup> 22 </servlet> 23 <servlet-mapping> 24 <servlet-name>SpringMVC</servlet-name> 25 <url-pattern>*.do</url-pattern> 26 </servlet-mapping> 27 </web-app>web.xml
1 package com.xcz.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 public class LoginController { 8 @RequestMapping("/login.do") 9 public String Login() { 10 return "login"; 11 } 12 }controller