SpringMVC_01 前期配置

前言-爲什麼要寫這個博客

本人之前零零散散的學過一些ssm的東西,現在開始接觸項目,發現有點難受,,,,,是相當難受,每一處看起來都是似曾相識, 但是真正上手的時候就很費力。

所以決定把這些東西在重新過一遍,以後陸續更新spring,mybatis。

本系列包括項目代碼以及在學習過程中所遇到的比較難搞的錯誤,歡迎各路大佬前來指教。

springmvc介紹

這部分寫了也是廢話,估計也不會有人看。有興趣瞭解的朋友可以自行百度。
在進行springmvc之前最好有一些spring的基礎,畢竟基於spring的註解。

實現的整體流程

項目結構:
在這裏插入圖片描述

- 加入對應的springMVC的jar包(文章頂部下載)
在這裏插入圖片描述
- 在web.xml中註冊springMVC配置文件的相關信息

<?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_3_0.xsd" 
	id="WebApp_ID" version="3.0">
  	
  	<!-- 配置DispatcherServlet -->
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		
		<!-- 配置DispatcherServlet 的一個初始化參數:配置SpringMVC配置文件的位置和名稱 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

- 添加springMVC的配置文件(上圖中的springmvc.xml,建議安裝spring插件)

引入的命名空間:
在這裏插入圖片描述

本部分代碼使用是要將自動配置包的路徑換位你的@corller註解所在的包:

<?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"
	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-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 配置自動掃描的包 -->
	<context:component-scan base-package="cn.sycu.sunqg.handler"></context:component-scan>

	<!-- 配置視圖解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

- 編寫請求處理器,並添加標識

package cn.sycu.sunqg.handler;

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

@Controller
public class helloWord {

	@RequestMapping("/helloWord")
	public String hello(){
		System.out.print("hello word ");
		return "success";
	}
}

- 編寫視圖

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<a href="helloWord">點擊這裏</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4>success!</h4>
</body>
</html>

總結

1.使用流程:

  • 新建web項目
  • 在web.xml中註冊springMVC的配置文件
  • 新建並編寫配置文件(必須爲spring類型)
  • 新建並編寫請求處理器並添加標識
  • 編寫視圖文件

2.springMVC處理流程

  • 使用requestMapping來映射請求

  • 返回值會通過視圖解析器解析爲實際的物理視圖,對於InternalResourceViewResolver視圖解析器,作出如下解析:
    通過這樣 prefix + return + 後綴 的方式得到時節的物理視圖

     	本例;/WEB-INF/views/success.jsp
    

如有問題可進行私信或評論

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章