MVC设计模式概览

实现MVC(Model,View,Controller)模式的应用程序由3大部分构成:

-模型:封装应用程序的数据和业务逻辑(POJO,Plain Old Java Object)

-视图,实现应用程序的信息显示功能(Jsp)

-控制器,接收来自用户的输入,调用模型层,,响应对应的视图组件Servlet,Filter。

下面看代码:

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <a href="product-input.action">Product Input</a>
  11. </body>
  12. </html>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  3. <display-name>struts2-1</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12. </web-app>

input.jsp

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="product-save.action" method="post">
  11. ProductName:<input type="text" name="productName">
  12. <br><br>
  13. ProductDesc:<input type="text" name="productDesc">
  14. <br><br>
  15. ProductPrice:<input type="text" name="productPrice">
  16. <br><br>
  17. <input type="submit" value="Submit">
  18. <br><br>
  19. </form>
  20.  
  21. </body>
  22. </html>
  1. package logan.struts.study;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.Filter;
  5. import javax.servlet.FilterChain;
  6. import javax.servlet.FilterConfig;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.ServletRequest;
  9. import javax.servlet.ServletResponse;
  10. import javax.servlet.annotation.WebFilter;
  11. import javax.servlet.http.HttpServletRequest;
  12.  
  13. /**
  14. * Servlet Filter implementation class FilterDispatcher
  15. */
  16. @WebFilter("*.action")
  17. public class FilterDispatcher implements Filter {
  18.  
  19. /**
  20. * Default constructor.
  21. */
  22. public FilterDispatcher() {
  23. // TODO Auto-generated constructor stub
  24. }
  25.  
  26. /**
  27. * @see Filter#destroy()
  28. */
  29. public void destroy() {
  30. // TODO Auto-generated method stub
  31. }
  32.  
  33. /**
  34. * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
  35. */
  36. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  37.  
  38. HttpServletRequest req = (HttpServletRequest) request;
  39.  
  40. //1.获取servletPath
  41. String servletPath = req.getServletPath();
  42. System.out.println(servletPath);
  43. //2.判断servletPath,若其等于"/product-input.action"则转发到
  44. ///WEB-INF/pages/input.jsp
  45. String path = null;
  46. if("/product-input.action".equals(servletPath)){
  47. path = "/WEB-INF/pages/input.jsp";
  48. }
  49.  
  50. if(path != null){
  51. request.getRequestDispatcher(path).forward(request, response);
  52. return;
  53. }
  54. chain.doFilter(request, response);
  55. }
  56.  
  57. /**
  58. * @see Filter#init(FilterConfig)
  59. */
  60. public void init(FilterConfig fConfig) throws ServletException {
  61. // TODO Auto-generated method stub
  62. }
  63.  
  64. }

在浏览器里面输入http://localhost:8080/struts2-1/product-input.action

可以看到

添加代码如下:

Product.java

  1. package logan.struts.study;
  2.  
  3. public class Product {
  4.  
  5. private Integer productId;
  6. private String productName;
  7. private String productDesc;
  8.  
  9. private double productPrice;
  10.  
  11. public Integer getProductId() {
  12. return productId;
  13. }
  14.  
  15. public void setProductId(Integer productId) {
  16. this.productId = productId;
  17. }
  18.  
  19. public String getProductName() {
  20. return productName;
  21. }
  22.  
  23. public void setProductName(String productName) {
  24. this.productName = productName;
  25. }
  26.  
  27. public String getProductDesc() {
  28. return productDesc;
  29. }
  30.  
  31. public void setProductDesc(String productDesc) {
  32. this.productDesc = productDesc;
  33. }
  34.  
  35. public double getProductPrice() {
  36. return productPrice;
  37. }
  38.  
  39. public void setProductPrice(double productPrice) {
  40. this.productPrice = productPrice;
  41. }
  42.  
  43. public Product(Integer productId, String productName, String productDesc, double productPrice) {
  44. super();
  45. this.productId = productId;
  46. this.productName = productName;
  47. this.productDesc = productDesc;
  48. this.productPrice = productPrice;
  49. }
  50.  
  51. public Product() {
  52.  
  53. }
  54.  
  55. @Override
  56. public String toString() {
  57. return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc
  58. + ", productPrice=" + productPrice + "]";
  59. }
  60. }
  1. package logan.struts.study;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.Filter;
  5. import javax.servlet.FilterChain;
  6. import javax.servlet.FilterConfig;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.ServletRequest;
  9. import javax.servlet.ServletResponse;
  10. import javax.servlet.annotation.WebFilter;
  11. import javax.servlet.http.HttpServletRequest;
  12.  
  13. /**
  14. * Servlet Filter implementation class FilterDispatcher
  15. */
  16. @WebFilter("*.action")
  17. public class FilterDispatcher implements Filter {
  18.  
  19. /**
  20. * Default constructor.
  21. */
  22. public FilterDispatcher() {
  23. // TODO Auto-generated constructor stub
  24. }
  25.  
  26. /**
  27. * @see Filter#destroy()
  28. */
  29. public void destroy() {
  30. // TODO Auto-generated method stub
  31. }
  32.  
  33. /**
  34. * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
  35. */
  36. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  37.  
  38. HttpServletRequest req = (HttpServletRequest) request;
  39.  
  40. //1.获取servletPath
  41. String servletPath = req.getServletPath();
  42. System.out.println(servletPath);
  43. //2.判断servletPath,若其等于"/product-input.action"则转发到
  44. ///WEB-INF/pages/input.jsp
  45. String path = null;
  46. if("/product-input.action".equals(servletPath)){
  47. path = "/WEB-INF/pages/input.jsp";
  48. }
  49.  
  50. //3.若其等于"/product-save.action"则
  51. if("/product-save.action".equals(servletPath)){
  52. //1).获取请求参数
  53. String productName = request.getParameter("productName");
  54. String productDesc = request.getParameter("productDesc");
  55. String productPrice = request.getParameter("productPrice");
  56.  
  57. //2).把请求信息封装为一个Product对象
  58. Product product = new Product(null, productName, productDesc, Double.parseDouble(productPrice));
  59. product.setProductId(1001);
  60. //3).执行保存操作
  61. System.out.println("Save Product" + product);
  62.  
  63. //4).把Product对象保存到request中。${param.productName} -> ${requestScope.product.productName}
  64. request.setAttribute("product", product);
  65. path = "/WEB-INF/pages/details.jsp";
  66. }
  67.  
  68. if(path != null){
  69. request.getRequestDispatcher(path).forward(request, response);
  70. return;
  71. }
  72. chain.doFilter(request, response);
  73. }
  74.  
  75. /**
  76. * @see Filter#init(FilterConfig)
  77. */
  78. public void init(FilterConfig fConfig) throws ServletException {
  79. // TODO Auto-generated method stub
  80. }
  81.  
  82. }

details.jsp

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. productId:${requestScope.product.productId }
  11. <br><br>
  12. productName:${requestScope.product.productName }
  13. <br><br>
  14. productDesc:${requestScope.product.productDesc }
  15. <br><br>
  16. productPrice:${requestScope.product.productPrice }
  17. <br><br>
  18. </body>
  19. </html>

当提交表单时

使用Filter作为控制器的好处:

使用一个过滤器来作为控制器,可以方便的在应用程序里对所有资源(包括静态资源)进行控制访问。

<url-pattern>*.action</url-pattern>

Servlet和Filter哪个更好?

Servlet能做的Filter都能做。

Filter能做的Servlet不一定能做,例如拦截资源。

Struts学习第一课 使用Filter作为控制器的MVC应用的更多相关文章

  1. Magento学习第一课——目录结构介绍

    Magento学习第一课--目录结构介绍 一.Magento为何强大 Magento是在Zend框架基础上建立起来的,这点保证了代码的安全性及稳定性.选择Zend的原因有很多,但是最基本的是因为zen ...

  2. Elasticsearch7.X 入门学习第一课笔记----基本概念

    原文:Elasticsearch7.X 入门学习第一课笔记----基本概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...

  3. [原创]java WEB学习笔记53:Struts2学习之路---前奏:使用 Filter 作为控制器的 MVC

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. MFC学习-第一课 MFC运行机制

    最近由于兴趣爱好,学习了孙鑫的MFC教程的第一课.看完视频了,自己便用visual studio 2010尝试了MFC编程,其中遇到了一些问题. 1.vs2010不像vs6.0那样可以新建一个空的MF ...

  5. python学习第一课要点记录

    写在要点之前的一段话,留给将来的自己:第一次参加编程的培训班,很兴奋很激动,之前都是自己在网上找免费的视频来看,然后跟着写一些课程中的代码,都是照着模子写,没有自己过多的思考.感觉这样学不好,除了多写 ...

  6. python学习第一课

    第一课: 1.不要使用来路不明的软件 2.下载杀毒软件 3.不懂技术的人在技术人面前会显得愈发无知 4.python无所不能 需要掌握的知识: 1.python基本语法 2.文件处理 3.函数 4.模 ...

  7. Asp.net MVC4高级编程学习笔记-视图学习第一课20171009

    首先解释下:本文只是对Asp.net MVC4高级编程这本书学习记录的学习笔记,书本内容感觉挺简单的,但学习容易忘记,因此在边看的同时边作下了笔记,可能其它朋友看的话没有情境和逻辑顺序还请谅解! 一. ...

  8. Kotlin学习第一课:从对比Java开始

    1. 介绍 今年初,甲骨文再次对谷歌所谓的安卓侵权使用Java提起诉讼,要求后者赔偿高达90亿美元.随后便传出谷歌因此计划将主力语言切换到苹果主导的Swift,不过这事后来没了跟进. 但谷歌在这两天的 ...

  9. Git速成学习第一课:创建版本库与版本回退

    Git速成学习笔记整理于廖雪峰老师的官网网站:https://www.liaoxuefeng.com/ 我太困了0.0精神点再写...... /*我来啦!以后会陆续更新自己的学习笔记*/ Git是分布 ...

随机推荐

  1. web框架之Django<一、初识>

    一.什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的 ...

  2. 第一篇 先用socket模拟web服务器

    一.用socket来模拟网站访问 socket为python2.7 #!/usr/bin/env python # -*- coding:utf-8 -*- import socket def han ...

  3. Vim 分隔窗口

    一,分隔窗口: 打开文件时在:命令模型时下面输入:split 将分隔为上下2个窗口:默认上窗口为活动窗口,可以通过CTRL-w来来回切换窗口; :close 为关闭窗口,最后一个窗口不能关闭: :on ...

  4. MySQL- 用Navicat通过隧道连接到远程数据库

    在企业中,为了安全地使用服务器,常常是用通过堡垒机才能连接到企业内部的服务器,当然也包括数据库. 于是我们时时需要通过堡垒机打隧道连到数据库,下面展示如何使用xshell用Navicat通过隧道连接到 ...

  5. 分享知识-快乐自己:Struts2 拦截器 与 过滤器

    拦截器的使用以及配置: package com.gdbd.interceptor; import com.gdbd.pojo.UserInfo; import com.opensymphony.xwo ...

  6. 分享知识-快乐自己:springboot之thymeleaf (1):简单的thymeleaf例子

    之前搞springboot时,发现spring很推荐thymeleaf,所以看了看学了学,感觉不错,做个笔记先. 做个简单和例子,项目是springboot,所以引入themeleaf相关包 pom. ...

  7. 一個在WCF學習中的小教訓(本人非科班菜鳥,此經驗無參考價值,衹是自己的經驗記錄)

    1.关于“ServiceHost 仅支持类服务类型”的解决:   Service属性必须执行,不是接口. 改为下图所示: 解决! (注:按朱哥的方法WCF已经可以通信---截至今天的11:11(例子在 ...

  8. Pyton基础-base64加解密

    base64加密后是可逆的,所以url中传输参数一般用base64加密 import base64 s='username=lanxia&username2=zdd' new_s=base64 ...

  9. 如何在MySQl数据库中给已有的数据表添加自增ID?

    由于使用MySQL数据库还没有多久的缘故,在搭建后台往数据库导入数据的时候发现新增的表单是没有自增id的,因次就有了上面这个问题. 解决方法 1.给某一张表先增加一个字段,这里我们就以node_tab ...

  10. 股神小L

    题解 贪心 若当前手中还持有股,则一定会卖出去. 否则,考虑之前卖出的最便宜的股,若售价比当前的股高,就买下这个股,否则我们就把之前卖出的最便宜的股改为买入,这样一定会有股,然后再把这个股卖出即可. ...