初识MVC模式

一.概念:MVC是 模型(Model),视图(View)和控制(Controller)的缩写,其目的实现Web系统的职能分工。

二.问题描述:通过一个简单的例子(计算圆面积)来体会MVC模式和普通的servlet+jsp的区别……

初识MVC模式

ctrl+"/'是单行注释,只在java文件里有效,在jsp里无效,不过可以撤销;ctrl+shift+"/"在java和jsp文件里都有效果,在jsp文件里是html注释,不过不可撤销,只能ctrl+z撤消了……

  1. jsp+javabean

  

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"

 2     pageEncoding="UTF-8"%>

 3 <html>

 4 <head>

 5     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 6     <title>JSP+JavaBean</title>

 7 </head>

 8 <body>

 9 <!-- 不写action的话是提交到本页面 -->

10 <form >

11     <input type="text" name="r"/>

12     <input type="submit" value="提交"/>

13 </form>

14 <jsp:useBean id="c" class="bean.Circle"/>

15 <jsp:setProperty property="*" name="c"/>

16 <!-- 从上下文获取 ,原来显示0.0-->

17 <jsp:getProperty property="area" name="c"/>

18 </body>

19 </html>

初识MVC模式

2.Cicle类

 1 package bean;

 2 

 3 public class Circle {

 4 

 5     private double r;

 6     private double area;

 7     

 8     public double getR() {

 9         return r;

10     }

11     public void setR(double r) {

12         this.r = r;

13     }

14     //面积不可以设定

15     public double getArea() {

16         area = Math.PI * r* r;

17         return area;

18     }

19 }

 3.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>test</display-name>

  

  <servlet>

  	<servlet-name>com</servlet-name>

  	<servlet-class>servlet.Com</servlet-class>

  </servlet>

  

  <servlet-mapping>

  	<servlet-name>com</servlet-name>

  	<url-pattern>/com</url-pattern>

  </servlet-mapping>

  

  

  <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>

</web-app>

 

4.输入界面

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>输入界面</title>

</head>

<body>

<!--com是servlet中Com.java的名字,随便起  -->

<form action="com">

	<input type="text" name="r"/>

	<input type="submit" value="提交"/>

</form>

</body>

</html> 

 

5.输出界面

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>输出界面</title>

</head>

<body>

r:${r }

<br/>

area:${area }

</body>

</html>

 

6.servlet文件

 1 package servlet;

 2 

 3 import java.io.IOException;

 4 

 5 import javax.servlet.ServletException;

 6 import javax.servlet.http.HttpServlet;

 7 import javax.servlet.http.HttpServletRequest;

 8 import javax.servlet.http.HttpServletResponse;

 9 

10 import bean.Circle;

11 

12 public class Com extends HttpServlet {

13 

14     @Override

15     protected void service(HttpServletRequest request, HttpServletResponse response)

16             throws ServletException, IOException {

17         String str = request.getParameter("r");

18         double r = Double.parseDouble(str);

19         

20         Circle c = new Circle();

21         c.setR(r);

22         double area = c.getArea();

23         

24         request.setAttribute("r", str);

25         request.setAttribute("area", area);

26         request.getRequestDispatcher("output.jsp").forward(request, response);

27         

28     }

29 

30     

31 }

 

你可能感兴趣的:(mvc)