Springmvc-Springmvc註解開發簡單實例

題外話

這幾天看Springmvc…好多好亂…光配置文件就一堆…

正文

springmvc註解,通過在java類中寫註解(@)的方法來代替在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_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>springmvc-2</display-name>

	<!-- 中央控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- 不要配置/*,否則404 -->
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

</web-app>

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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	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-3.0.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.0.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
	<!-- 註解文件形式要配置的組件:ViewResolver,interceptor -->

	<!-- <mvc:annotation-driven /> mvc註解的驅動,但是如果context:component-scan存在,mvc:annotation-driven就沒有必要配置了 -->
	<context:component-scan base-package="com.controller"></context:component-scan>

	<!-- 視圖解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置從項目根目錄到指定目錄一端路徑 -->
		<!-- 文件的前面部分 -->
		<!-- 不能配置太深的目錄,否則之前的目錄無法訪問 -->
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<!-- 文件的後綴名 -->
		<property name="suffix" value=".jsp"></property>
	</bean>



</beans>

.java

package com.controller;

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

@Controller
@RequestMapping("/test") //相似於命名空間,唯一標識一個Controller,防止名稱重複
public class TestAnnController {

	/**
	 * desc 返回字符串含義,指的是ModelAndView中的ViewName,也就是要跳轉的頁面
	 * 
	 * @RequestMapping 請求的映射 映射到一個具體的方法 同value來指定,如果不寫value也是默認給value賦值
	 * @return String
	 */
	@RequestMapping("/hello.do")
	public String hello() {
		System.out.println("hello springmvc with annotation!");
		return "index";

	}

}

.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>My JSP 'index.jsp' starting page</title>

</head>

<body>hello Springmvc with Annotation!
</body>
</html>

效果
在這裏插入圖片描述

現在發現,英語好是多麼重要,寫代碼都會輕鬆很多,很多地方一看就知道是幹什麼的.
這是我這幾天的筆記


1.	DispatcherServlet:中央控制器,把請求給轉發到具體的控制類
2.	Controller:具體處理請求的控制器
		ParameterizableViewController 參數控制器 
		AbstractCommandController 命令控制器 
		FormController 表單控制器
3.	handlerMapping:映射處理器,負責映射中央處理器轉發給controller時的映射策略
		BeanNameUrlHandlerMapping  
		SimpleUrlHandlerMapping
		ControllerClassNameHandlerMapping 
4.	ModelAndView(模型&視圖):服務層返回的數據和視圖層的封裝類
5.	ViewResolver:視圖解析器,解析具體的視圖
6.	Interceptors :攔截器,負責攔截我們定義的請求然後做處理工作

MVC 模型視圖控制器  
	
MVC三層架構,是一種設計模式
	M:Model 模型,代表着業務邏輯代碼與數據庫代碼,
	V:View 視圖 對數據的展示代碼,比如JSP頁面,就是專門用來展示數據,美化頁面的  
	C:Controller 控制器 控制,Servlet來充當這一角色,連接着View和Model,從View中獲得指令,
		在從model中通過業務邏輯代碼獲取需要的數據,然後在通過Servlet去交給View層去顯示。也就是MVC三層結構的意義
		
經典三層架構
	表示層(UI):通俗講就是展現給用戶的界面,即用戶在使用一個系統的時候他的所見所得。
 	業務邏輯層(BLL):針對具體問題的操作,也可以說是對數據層的操作,對數據業務邏輯處理。 
	數據訪問層(DAL):該層所做事務直接操作數據庫,針對數據的增添、刪除、修改、查找等。
	
mappings 映射
Framework 框架
Resolver 解析器
spring 春天
Parameter 參數
Abstract 抽象的
arg 函數(自變量)
Command 命令
property 屬性,性質
protected 保護,受保護的
form 表單
simple 簡單的
handler 處理器
config 配置
source 來源
thread 線程
display 顯示,展示
encoding 編碼
Internal 內部的
Resource 資源
annotation 註解,註釋
context 環境,上下文
component 成分,組件
scan  掃描
base 基礎,底層
desc 產品說明
preview 預覽
design 設計
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章