STRUTS学习笔记

Struts工作原理图

首先新建动态Web项目,如何在web.xml文件中配置过滤器,在struts.xml文件中配置action,编写action执行的函数,编写 函数执行完成后跳转的jsp

显示数据到jsp

原理图

1. 访问路径 /showProduct
2. 所有访问都被struts的filter所拦截,并进入struts的工作流程
3. 根据配置文件struts.xml, 会执行ProductAction的show方法
4. 在show方法中,将实例属性product指向一个新的对象,并设置名称为iphone7
5. 服务端跳转到show.jsp
6. 在show.jsp中,访问ProductAction.getProduct() 获取实例属性product,并显示其名称iphone7
Struts通过getProudct()方法返回product对象,然后再把product对象通过request.setAttribute("product", product) 放在"product" 这个key上,这样就方便EL表达式去取出来了
 
练习:访问路径 /showTime 跳转到页面 showTime.jsp,并且在showTime.jsp中显示当前时间
 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <%@ page import="java.util.*"%>
 4 <%@ page import="java.text.*"%>
 5 <% 
 6 String datetime=new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(Calendar.getInstance().getTime()); //获取系统时间 
 7 request.setAttribute("date",datetime);
 8 %>
 9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
10 <html>
11 <head>
12 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
13 <title>Insert title here</title>
14 </head>
15 <body>
16     ${date};
17 </body>
18 </html>

 

 

STRUTS 提交数据到ACTION

1. 访问路径 /addProduct
2. 所有访问都被struts的filter所拦截,并进入struts的工作流程
3. 根据配置文件struts.xml, 会执行ProductAction的add方法
4. 在add方法执行之前,Struts生成一个新的product对象,并把页面传递过来的name设置在该对象上,接着把该对象通过setProduct()方法,注入ProductAction
5. 服务端跳转到show.jsp
6. 在show.jsp中,访问ProductAction.getProduct() 获取注入的product,并显示其名称

 

 

posted @ 2018-03-22 10:04  暖熊熊  阅读(76)  评论(0编辑  收藏  举报