中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

一個(gè)SpringMVC的入門(mén)實(shí)例

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

1 介紹


下面是我拷的,介紹MVC.

MVC框架是什么

模型-視圖-控制器(MVC)是一個(gè)眾所周知的以設(shè)計(jì)界面應(yīng)用程序?yàn)榛A(chǔ)的設(shè)計(jì)模式。它主要通過(guò)分離模型、視圖及控制器在應(yīng)用程序中的角色將業(yè)務(wù)邏輯從界面中解耦。通常,模型負(fù)責(zé)封裝應(yīng)用程序數(shù)據(jù)在視圖層展示。視圖僅僅只是展示這些數(shù)據(jù),不包含任何業(yè)務(wù)邏輯?刂破髫(fù)責(zé)接收來(lái)自用戶的請(qǐng)求,并調(diào)用后臺(tái)服務(wù)(manager或者dao)來(lái)處理業(yè)務(wù)邏輯。處理后,后臺(tái)業(yè)務(wù)層可能會(huì)返回了一些數(shù)據(jù)在視圖層展示。控制器收集這些數(shù)據(jù)及準(zhǔn)備模型在視圖層展示。MVC模式的核心思想是將業(yè)務(wù)邏輯從界面中分離出來(lái),允許它們單獨(dú)改變而不會(huì)相互影響。

在Spring MVC應(yīng)用程序中,模型通常由POJO對(duì)象組成,它在業(yè)務(wù)層中被處理,在持久層中被持久化。視圖通常是用JSP標(biāo)準(zhǔn)標(biāo)簽庫(kù)(JSTL)編寫(xiě)的JSP模板。控制器部分是由dispatcher servlet負(fù)責(zé),在本教程中我們將會(huì)了解更多它的相關(guān)細(xì)節(jié)。
一些開(kāi)發(fā)人員認(rèn)為業(yè)務(wù)層和DAO層類是MVC模型組件的一部分。我對(duì)此持有不同的意見(jiàn)。我不認(rèn)為業(yè)務(wù)層及DAO層類為MVC框架的一部分。通常一個(gè)web應(yīng)用是3層架構(gòu),即數(shù)據(jù)-業(yè)務(wù)-表示。MVC實(shí)際上是表示層的一部分。


Dispatcher Servlet(Spring控制器)

在最簡(jiǎn)單的Spring MVC應(yīng)用程序中,控制器是唯一的你需要在Java web部署描述文件(即web.xml文件)中配置的Servlet。Spring MVC控制器 ——通常稱作Dispatcher Servlet,實(shí)現(xiàn)了前端控制器設(shè)計(jì)模式。并且每個(gè)web請(qǐng)求必須通過(guò)它以便它能夠管理整個(gè)請(qǐng)求的生命周期。

當(dāng)一個(gè)web請(qǐng)求發(fā)送到Spring MVC應(yīng)用程序,dispatcher servlet首先接收請(qǐng)求。然后它組織那些在Spring web應(yīng)用程序上下文配置的(例如實(shí)際請(qǐng)求處理控制器和視圖解析器)或者使用注解配置的組件,所有的這些都需要處理該請(qǐng)求。


在Spring3.0中定義一個(gè)控制器類,這個(gè)類必須標(biāo)有@Controller注解。當(dāng)有@Controller注解的控制器收到一個(gè)請(qǐng)求時(shí),它會(huì)尋找一個(gè)合適的handler方法去處理這個(gè)請(qǐng)求。這就需要控制器通過(guò)一個(gè)或多個(gè)handler映射去把每個(gè)請(qǐng)求映射到handler方法。為了這樣做,一個(gè)控制器類的方法需要被@RequestMapping注解裝飾,使它們成為handler方法。

handler方法處理完請(qǐng)求后,它把控制權(quán)委托給視圖名與handler方法返回值相同的視圖。為了提供一個(gè)靈活的方法,一個(gè)handler方法的返回值并不代表一個(gè)視圖的實(shí)現(xiàn)而是一個(gè)邏輯視圖,即沒(méi)有任何文件擴(kuò)展名。你可以將這些邏輯視圖映射到正確的實(shí)現(xiàn),并將這些實(shí)現(xiàn)寫(xiě)入到上下文文件,這樣你就可以輕松的更改視圖層代碼甚至不用修改請(qǐng)求handler類的代碼。
為一個(gè)邏輯名稱匹配正確的文件是視圖解析器的責(zé)任。一旦控制器類已將一個(gè)視圖名稱解析到一個(gè)視圖實(shí)現(xiàn)。它會(huì)根據(jù)視圖實(shí)現(xiàn)的設(shè)計(jì)來(lái)渲染對(duì)應(yīng)對(duì)象。


2 導(dǎo)入jar包

至少應(yīng)該有這些.



3 配置文件

3.1 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_HelloWorld</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- spring mvc 的servlet -->
  <!-- DispatcherServlet在初始化后會(huì)直接在/WEB-INF/下找springmvc-servlet.xml文件,
               servlet-name標(biāo)簽的參數(shù)定義要和XML文件對(duì)應(yīng) -->
  <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>
  
  <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>


3.2 springmvc-servlet.xml

這個(gè)文件的名字是由web.xml里面配置的DispatcherServlet的<servlet-name></servlet-name>決定的,路徑在上下文/WEB-INF/里面,主要是配置控制器返回的邏輯視圖名和物理視圖的對(duì)應(yīng)關(guān)系

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                     http://www.springframework.org/schema/beans/spring-beans.xsd 
                     http://www.springframework.org/schema/tx 
                     http://www.springframework.org/schema/tx/spring-tx.xsd 
                     http://www.springframework.org/schema/aop 
                     http://www.springframework.org/schema/aop/spring-aop.xsd
                     http://www.springframework.org/schema/context 
                       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 自動(dòng)掃描的包 -->
    <context:component-scan base-package="com.lin.helloworld.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <!-- controller 返回的一個(gè)邏輯視圖名經(jīng)過(guò)前后綴的處理返回物理視圖 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>


4 編寫(xiě)一個(gè)domain類

用來(lái)封裝一些提交數(shù)據(jù)

package com.lin.helloworld.domain;

public class HelloWorld {
    
    private String data;

    public HelloWorld() {
        super();
    }

    public HelloWorld(String data) {
        super();
        this.data = data;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "HelloWorld [data=" + data + "]";
    }

}


5 編寫(xiě)controller

這個(gè)是MVC中的控制器,和struts2不一樣的是他是方法級(jí)的攔截,struts2是類級(jí)的攔截.

package com.lin.helloworld.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.lin.helloworld.domain.HelloWorld;

@Controller
public class HelloWorldController {

    //這里的/hello相當(dāng)于struts2里的一個(gè)action
    //返回一個(gè)字符串給視圖
    @RequestMapping("/hello")
    public ModelAndView sayHello()
    {
        //modelAndView的構(gòu)造方法的第一個(gè)參數(shù)相當(dāng)于Struts2里的一個(gè)result的name
        ModelAndView modelAndView = new ModelAndView("helloworld", "msg", "HelloWorld!!!");
        return modelAndView;
    }
    
    //返回一個(gè)對(duì)象給視圖
    //@ModelAttribute("obj")的作用相當(dāng)于Struts2的action類里面的一個(gè)field,
    //用于表單提交的數(shù)據(jù)放進(jìn)一個(gè)對(duì)象里面
    //這里和struts2的區(qū)別:
    //struts2處理表單提交的方式是:<input name="obj.data"/> 提交的數(shù)據(jù)封裝在obj對(duì)象的data里面
    //springmvc的方式是:<input name="data"/> 提交的數(shù)據(jù)封裝在obj對(duì)象的data里面,
    //前提是要使用@ModelAttribute注解
    @RequestMapping("/helloObj")
    public ModelAndView sayHelloWorld(@ModelAttribute("obj") HelloWorld obj)
    {
        System.out.println(obj.toString());
        ModelAndView modelAndView = new ModelAndView("helloworld", "obj", obj);
        return modelAndView;
    }

}


6 視圖

<%@ 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>My JSP 'helloworld.jsp' starting page</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>
    HelloWorld! This is a spring mvc framework example.<br>
    ${msg}
    <hr/>
    <form action="helloObj" method="post">
        <!-- 這里的表單提交和struts2不同的是name="data"會(huì)自動(dòng)對(duì)應(yīng)上對(duì)象的filed -->
        <input type="text" name="data" size="30"/><br/>
        <input type="submit" value="Submit"/>
    </form>
    <hr/>
    ${obj.data}
  </body>
</html>


7 目錄結(jié)構(gòu)



 

標(biāo)簽: isp 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:java實(shí)現(xiàn)選擇排序算法

下一篇:最全的常用正則表達(dá)式