MVC初探

4个步骤:

①分析用户视图及高层体系结构

②创建用于开发这个项目的开发环境

③创建用于部署这个项目的部署环境

④对各个组件完成迭代的开发和测试


用户视图

form.html

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1 align="center">Beer Selection Page</h1>
<form method="post"
	action="SelectBeer.do">
	Select beer characteristics<p>
	Color:
	<select name="color" size="1">
		<option>light
		<option>amber
		<option>brown
		<option>dark
	</select>
	<br><br>
	<center>
		<input type="SUBMIt">
	</center>
</form>

</body>
</html>

result.jsp

<%@ 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">
<%@ page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<h1 align="center">Beer Recommendations JSP</h1>
<p>

<%
	List styles = (List)request.getAttribute("styles");
	Iterator it = styles.iterator();
	while(it.hasNext()){
		out.print("<br>try: "+it.next());
	}
%>
</body>
</html>



体系结构:

1)用户请求得到form.html页面

2)容器获得form.html页面

3)容器把这个页面返回给浏览器,用户再在浏览器上回答表单上的问题

4)浏览器把请求数据发给容器

5)容器根据URL找到正确的servlet,并把请求传递给这个servlet

6)servlet调用BeerExpert寻求帮助

7)这个“专家”类返回一个回答,servlet把这个回答增加到请求对象

8)servlet把请求转发给JSP

9)JSP从请求对象得到回答

10)JSP为容器生成一个页面

11)容器把这个页面返回给用户


开发环境




配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4">
  <servlet>
    <servlet-name>Ch3 Beer</servlet-name>
    <servlet-class>com.example.web.BeerSelect</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Ch3 Beer</servlet-name>
    <url-pattern>/SelectBeer.do</url-pattern>
  </servlet-mapping>
</web-app>

模型

BeerExpert.java

package com.example.model;
import java.util.ArrayList;
import java.util.List;
public class BeerExpert {
	public List getBrands(String color){
		List brands = new ArrayList();
		if(color.equals("amber")){
			brands.add("Jack Amber");
			brands.add("Red Moose");
		}else{
			brands.add("Jail Pale Ale");
			brands.add("Gout Stout");
		}
		return (brands);
	}
}



控制器

BeerSelect.java

package com.example.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.model.BeerExpert;

/**
 * Servlet implementation class BeerSelect
 */
@WebServlet("/BeerSelect")
public class BeerSelect extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BeerSelect() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//这个方法来自ServletResponse接口
		//response.setContentType("text/html");
		//PrintWriter out = response.getWriter();
		//out.println("Beer Selection Advice<br>");
		//这个方法来自ServletRequest接口。参数与html<select>标记中name属性值一致
		String c = request.getParameter("color");
		//测试结果显示
		//out.println("<br>Got beer color "+c);
		BeerExpert be = new BeerExpert();
		List result = be.getBrands(c);
		//为请求对象添加一个属性,供jsp使用。jsp要寻找styles
		request.setAttribute("styles", result);
		//为jsp实例化一个请求分派器
		RequestDispatcher view = request.getRequestDispatcher("result.jsp");
		//使用请求分派器要求容器准备好jsp,并向jsp发送请求和响应
		view.forward(request, response);
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值