SpringMVC簡單配置
阿新 • • 發佈:2018-12-18
執行流程:
頁面請求---->web.xml---->DispatcherServlet---->HandlerMapping---->Controller---->ModelAndView---->ViewResolver---->View
一、配置檔案實現MVC
1、匯入相應的jar包
2、配置web.xml檔案:配置DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?> <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"> <servlet> <servlet-name>springMVC</servlet-name> <!-- 在<servlet>標籤中配置DispatcherServlet的訪問路徑 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 指定springMVC的地址 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> a. servlet-class的值是一個jar包中的DispatcherServlet類 b. <init-param>標籤中的<param-name>標籤中寫入contextConfigLocation這個值是固定的,他是dispathcherServlet中的一個屬性。<param-value>標籤中寫入classpath:SpringMVC的配置檔案地址。 c. 配置<load-on-startup>值為1即在專案啟動時建立servlet物件 d. 配置servlet-mapping
3、配置Spring配置檔案:配置HandlerMapping、ViewResolver
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <bean id="loginController" class="com.bb.controller.LoginController"></bean> <!-- 定義HandlerMapping,用於請求的對映 --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="login.do">loginController</prop> </props> </property> </bean> <!-- 定義ViewResolver,實現檢視對映 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <!-- 字首 --> <property name="prefix" value="/"></property> <!-- 字尾 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
4、模擬建立一個Controller類
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.ui.ModelMap; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class LoginController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); String password = req.getParameter("password"); System.out.println(username + ", " + password); if("老羅".equals(username) && "1234".equals(password)) { //根據配置檔案訪問/success.jsp return new ModelAndView("success"); } // 轉發錯誤資訊 ModelMap map = new ModelMap(); map.put("error", "使用者名稱或密碼錯誤"); //根據配置檔案訪問/index.jsp return new ModelAndView("index", map); } }
5、一個簡單的jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登入</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login.do">
使用者名稱<input name="username"><span style="color:red;">${error}</span><br>
密碼<input type="password" name="password"><br>
<input type="submit" value="登入">
</form>
</body>
</html>
二、註解實現MVC
1、jar包必不可少
2、web.xml和上面的一樣
3、Spring配置檔案裡只需要兩行
<!-- 開啟IOC註解 -->
<context:component-scan base-package="com.oracle"></context:component-scan>
<!-- 開啟MVC註解 -->
<mvc:annotation-driven></mvc:annotation-driven>
4、Controller類就不一樣嘍,這次我們不需要去實現Controller介面了。
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
@Controller
@RequestMapping("/user")
public class LoginController {
@RequestMapping("/login.do")
@ResponseBody
public String login(String username ,String password) {
if("老羅".equals(username) && "1234".equals(password)) {
//向頁面返回success
return "success";
}
//向頁面返回失敗資訊
return "fail";
}
}
如果不加@ResponseBody return返回值則內部消化
return "/index.jsp"; //轉發到index.jsp頁面
return "redirect:/index.jsp"; //重定向到index.jsp頁面
return "/user/login.do"; //轉發給/user/login.do方法
return "redirect:/user/login.do"; //重定向到/user/login.do方法
5、這回頁面就需要通過js進行請求和判斷了,很簡單,我就不寫了!