1. 程式人生 > >Java自學之路-Java中級教程-12:SpringMVC的三層架構:模型層、表現層、控制層

Java自學之路-Java中級教程-12:SpringMVC的三層架構:模型層、表現層、控制層

MVC即Model、View、Controller三者的縮寫。Model為模型層,View為表現層,Controller為控制層。其中M處於最底層,V在最上層,中間層為Controller。比如使用者訪問網站,首先接觸的是View,即是網頁。通過訪問網頁的url,就會傳到Controller層,再由Controller層去調Model層。

MVC的三層架構表現為Java的jsp、bean、controller。其實controller和jsp都是servlet,jsp由tomcat執行後會生成.java檔案,裡面的結構和controller差不多,都是處理HttpServletRequest和HttpServletResponse。

springMVC框架提供了Controller層,而Model層由spring jdbc或其他框架承擔資料訪問操作,jsp則是獨立的。使用springMVC、spring、springJDBC這三個類庫,就可以輕鬆搭建具有MVC模式的Web工程。

結合前面的Dao層、Service層,再加上Controller層,整個Web工程的結構就很完備了。現在把Dao層的類和xml都加到Web工程裡來,並改變一下包的層次,分成com.helloworld.controller、com.helloworld.dao、com.helloworld.dao.impl、com.helloworld.model、com.helloworld.service、com.helloworld.service.impl,整個Web工程的包結構就更加清晰了。

這裡把PersonService也分成了一個介面類和實現類,和PersonDao介面是一樣的道理。

package com.helloworld.service;

import java.util.List;

import com.helloworld.model.Person;

public interface PersonService {

	public List helpToDo() throws Exception;

}
package com.helloworld.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.helloworld.dao.PersonDao;
import com.helloworld.model.Hand;
import com.helloworld.model.Person;
import com.helloworld.service.PersonService;

@Service
public class PersonServiceImpl implements PersonService {

	@Autowired
	private PersonDao personDao;

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public List helpToDo() throws Exception {
		List personList = personDao.getPersonList();
		for (Person person : personList) {
			Hand hand = person.getHand();
			// 讓每個Person幫手做一些事...
			if (hand != null) {
				System.out.println(hand.toString());
			} else {
				System.out.println("No hand!");
			}

		}
		return personList;
	}

}

在上一節的web.xml中,啟動Tomcat就會載入applicationContext-mvc.xml並初始化Controller。現在還要把applicationContext-dao.xml也載入進來,就可以在applicationContext-mvc.xml裡面加上import語句import resource="applicationContext-dao.xml,讓applicationContext-mvc.xml也包含applicationContext-dao.xml。

要在PersonController中呼叫Service,可以把PersonService注入給PersonController,使用註解@Autowired。同時,在PersonController中增加一個方法public String helpToDo(HttpServletRequest request, HttpServletResponse response),在這個方法裡面可以呼叫personService.helpToDo()來獲取資料記錄列表List personList。把獲取到的personList變數傳給request物件,使用request.setAttribute("personList", personList),以便在jsp網頁中顯示這些記錄。而WebRoot\jsp\person.jsp裡面只有<%=request.getAttribute("personList") %>這句。

package com.helloworld.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.helloworld.model.Person;
import com.helloworld.service.PersonService;

@Controller
@RequestMapping("/")
public class PersonController {

	@Autowired
	private PersonService personService;

	@RequestMapping(value = "/listPerson", method = RequestMethod.GET)
	public String listPerson(HttpServletRequest request, HttpServletResponse response) {
		String message = "Test message.";

		request.setAttribute("message", message);
		return "index.jsp";
	}

	@RequestMapping(value = "/helpToDo", method = RequestMethod.GET)
	public String helpToDo(HttpServletRequest request, HttpServletResponse response) {
		try {
			List personList = personService.helpToDo();
			request.setAttribute("personList", personList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "jsp/person.jsp";
	}

}

重新部署到Tomcat,開啟瀏覽器輸入地址http://localhost/calculateWeb/helpToDo,即可把所有資料記錄顯示出來了。但這裡面顯示的記錄都是些 public String toString() {
return this.id + "," + this.nation + ";";
}
 

<span font-size:medium;white-space:normal;"="" style="word-wrap: break-word; margin: 0px; padding: 0px;">1,USA;, 1,USA;, 1,USA;, 1,USA;

配套進階視訊教程:

Java中级SSHæ¡æ¶é¡¹ç®å¼å设计æç¨

Java視訊教程