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视频教程


猜你喜欢

转载自blog.csdn.net/weixin_41239710/article/details/80463543
今日推荐