spring-mvc 入門-一個簡單的例子
阿新 • • 發佈:2019-01-30
spring mvc 框架本質上是一個servlet,在深究springmvc 底層實現之前我們先進行一個簡單的springmvc入門例子,來帶領大家進行一個spring mvc 的初體驗。
1.環境搭建
spring mvc 的環境搭建非常簡單,首先建立一個web 專案,如果是maven專案,只需要簡單地加入spring mvc 和servlet的依賴就可以了(Tomcat8 預設使用的是servlet3.1,Tomcat使用的是servlet3.0)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId >javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5</version>
</dependency>
如果沒有使用maven,如果是myeclipse ,那麼直接引入spring 框架就可以了。
2.web.xml配置spring mvc
1.在web.xml 中配置spring mvc
<!-- spring mvc配置 開始 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- spring mvc配置 結束 -->
在配置DispatcherServlet 的時候可以設定contextConfigLocation引數來設定spring mvc 的配置檔案的位置,預設是使用web-inf下的[servlet-name]-servlet.xml
3.建立springmvc-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.home.sd" />
</beans>
如果只想掃描 @controller 的配置如下
<context:component-scan base-package="com.home.sd" use-default-filters="false">
<context:include-filter type="annotation" expression ="org.springframework.stereotype.Controller" />
</context:component-scan>
4.建立 Controller 和view
1.建立 Controller 類
首先在 com.home.sd包下建立一個類 StartController.java
package com.home.sd.controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class StartController {
private final Log logger = LogFactory.getLog(StartController.class);
@RequestMapping(value={"/"},method={RequestMethod.POST})
public String head(){
return "index.jsp";
}
@RequestMapping(value={"/index","/"},method ={RequestMethod.GET})
public String index(Model model) throws Exception{
logger.info("proccessed by index ======");
model.addAttribute("msg", "hello Spring mvc");
return "index.jsp";
}
}
2.建立view index.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>
伺服器資訊 :${msg}
</body>
</html>
好了,接下來將專案部署到Tomcat上,執行,就會出現以下效果: