首页 Struts2中Action从表单取值并且存到Web元素中(session)

Struts2中Action从表单取值并且存到Web元素中(session)

举报
开通vip

Struts2中Action从表单取值并且存到Web元素中(session)Struts2中Action从表单取值并且存到Web元素中(session) 在struts2中,Action不同于struts1.x中的Action。在struts2中Action并不需要继承任何控制器类型或实现相应接口。比如struts1.x中的Action需要继承Action或者DispatcherAction。 同时struts2中的Action并不需要借助于像struts1.x中的ActionForm获取表单的数据。可以直接通过与表单元素相同名称的数据成员,需要存在符合命名规范的set和get方法,获...

Struts2中Action从表单取值并且存到Web元素中(session)
Struts2中Action从表单取值并且存到Web元素中(session) 在struts2中,Action不同于struts1.x中的Action。在struts2中Action并不需要继承任何控制器类型或实现相应接口。比如struts1.x中的Action需要继承Action或者DispatcherAction。 同时struts2中的Action并不需要借助于像struts1.x中的ActionForm获取表单的数据。可以直接通过与表单元素相同名称的数据成员,需要存在符合命名 规范 编程规范下载gsp规范下载钢格栅规范下载警徽规范下载建设厅规范下载 的set和get 方法 快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载 ,获取页面表单数据。 虽然struts2中的Action原则上不用继承任何类型。但是一般需要实现 com.opensymphony.xwork2.Action接口或者继承 com.opensymphony.xwork2.ActionSupport类型,然后重写execute方法。通常更愿意去继承ActionSupport类型,这样我们可以在我们的控制器中增加更多的功能。因为ActionSupport本身不但实现了Action接口,而且实现了其他的几个接口,让我们的控制器的功能更加强大,例如: com.opensymphony.xwork2.Validateable:提供了validate方法,可以对action中的数据进行校验 com.opensymphony.xwork2.ValidationAware:提供了addFieldError方法,可以存取action级别或者字段级别的错误消息 com.opensymphony.xwork2.TextProvider:提供了获取本地化信息文本的方法getText com.opensymphony.xwork2.LocaleProvider:提供了getLocale方法,用于获取本地信息 从以上我们可以看到,继承ActionSupport,可以完成更多的工作。 例如上面的例子,例如我们需要判断输入文本框的内容,输入的内容长度必须在6-10之间。那么我们可以增加校验工作,利用validate方法。更改后的代码如下: package com.frank.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { private String message; private String username; @Override pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run public String execute() throws Exception { this.message="Hello World:"+this.username; return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public void validate() { if(this.username.trim().length()<6||this.username.trim().length()>10){ addFieldError("user.username","the length is invalid"); } } } 由于实现了validate方法,这样当请求一个控制器,在这里为helloWorld.action,的时 候,首先执行validate方法,如果有错误信息增加到action中,那么就不继续执行Action, 返回INPUT,否则就继续执行Action。在本例中,首先判断用户名称是否在合法的长度范 围,如果不在增加错误信息,返回INPUT,默认返回,。 因为有错误返回INPUT,所以此Action在配置文件中应该定义INPUT转发路径 display.jsp helloWorld.jsp pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run 出现错误时,返回到helloWorld.jsp,并在此页面中显示控制器中所注册的错误消息,更 改后的页面如下,黑色字体为增加的, <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib uri="/struts-tags" prefix="s" %> My JSP 'helloWorld.jsp' starting page


username:
当前Action执行的核心方法都是execute,有时我们可以能希望同一个控制器做不同的工 作。例如在对一个表分别进行CRUD操作时,我们可能需要根据不同的情况执行4种操作, pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run 这样一个execute做起来比较麻烦,我们可以定义我们自己的方法,然后控制器有选择的执行。类似struts1.x中的DispatcherAction控制器一样。 这时我们自定义的操作方法,必须和execute具有相同的签名,只是名称不同而已。例如在前面的例子中我们增加一个超链接,让它同样请求helloWorld.action,但是在控制器端执行自己定义的方法myExecute。注意在这里先去掉验证方法validate,取消验证。修改后代码如下: package com.frank.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { private String message; private String username; @Override public String execute() throws Exception { this.message="Hello World:"+this.username; return SUCCESS; } public String myExecute() throws Exception{ this.message="Good Morning !!!"; return SUCCESS; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run 此时我们需要增加新的Action配置,并指明调用的方法 display.jsp 在helloWorld.jsp中增加超链接,强求新增加的Action OtherAction 新的请求页面: 点击超链接后,请求新的Action,结果如下: 由于请求的新Action在配置中指明了所调用的方法为myExecute,所以在控制器端不再执行execute方法,转而执行myExecute,所以输出结果为 Good Morning !!! 同样存在另外一种形式请求新的Action: 即:Action名!新的方法名.action,刚才的超链接可以更改成:OtherAction 表单数据的提交 表单数据的提交在Web编程中是非常重要的。控制器需要获取用户在表示层,页面,所提交的数据,然后进行下一步操作。比如在用户登录操作中,控制器需要获取用户在页面中输入的“用户名称”和“密码”数据,来决定下一步的验证。 获取表单提交的数据有两种方式: pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run 第一种为使用中介对象,我通常把它称为包装表示层数据的包裹,。比如在strtus1.x中使 用各种ActionForm来封装页面数据。使用中介对象可以同时在中介对象的里面增加诸如: 验证、过滤、日志记录等附加工作。但是同样带来了类数量的膨胀,存在各种各样的 ActionForm类,LoginForm、PersonForm等等,,不利于项目的维护管理。 第二种是直接使用控制器中的域对象,即直接使用控制器中的数据成员获取表示层的数据, 在struts2种支持此种方式。但必须保证相应的数据成员和表示层提交的数据名称一致,并 且具有符合命名规范的setter和getter方法。这样可以达到“内省”。直接使用域对象可 以减少类的膨胀,如果只是简单的获取提交的数据则建议直接使用域对象。 在此我们还是同一个非常简单的,我经常采用的,登录操作来体会开发过程。可以通过此操 作扩展到复杂的操作,其实原理一样。还是从头开始,省去准备工作的步骤,直接进入开发 过程,: 首先创建一个登录页面login.jsp,代码内容如下: <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib uri="/struts-tags" prefix="s" %> My JSP 'login.jsp' starting page pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run

User Login


Username:
Password:
用于显示所注册的错误信息 接着建立Actiion类:LoginAction,在此我们通过域对象获取表示层提交的数据,也就是在控制器中声明两个数据成员username和password来得到表示层的数据,然后在 execute方法中进行验证工作,当然,一般规范开发需要在业务类中进行实际验证,,在此简便此操作。同时重写validate方法在Action实际执行前进行验证工作,具体的验证方式由自己的业务决定。代码如下: package com.frank.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private String username; private String password; @Override public String execute() throws Exception { if(this.username.equals("admin")&&this.password.equals("admin")){ return SUCCESS; } return INPUT; } @Override pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run public void validate() { if(this.username==null||this.username.trim().length()<1){ addFieldError("no.username","must input username"); } if(this.password==null||this.password.trim().length()<1){ addFieldError("no.password","must intpu password"); } } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 新建一个index.jsp页面,用户在用户登录成功后显示,内容非常的简单,就是一条“Login Success,,,” <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> My JSP 'index.jsp' starting page pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run

Login Success!!!

现在可以开始配置Action了,在struts.xml中增加新的Action,配置如下: index.jsp login.jsp 可以部署和运行了,启动login.jsp 如果在任意一个文本框中不输入内容或者输入空白内容,则返回当前页面,并错误提示 如果输入的用户名和密码错误,同样返回到当前页面 如果输入的用户和密码正确,则到达index.jsp pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run 注意以上所有的提交方式都为默认GET提交方式 封装表示层数据到业务类中 在实际的开发过程中,我们可以用业务类对象获取表示层数据,这样我们可以在业务类中实现更多的业务方法,比如登录、注册,。而由控制器去调用业务类。因为struts2同样遵循的是MVC模式,所以我们不应该让控制器去做实际的业务工作,而是调用Model,而这里同样用Model获取表示层的数据。 这样我们可以新建一个类User,在这里声明数据成员username和password,并增加一个验证方法login,用于验证登录。User类的代码如下: package com.frank.rule; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean login(){ if(this.username.equals("admin")&&password.equals("admin")){ return true; } return false; pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run } } 既然我们用User获取用户的数据,这样我们需要在Action增加User类型的数据成员, 而去掉原来的username和password,重构的代码如下: package com.frank.action; import com.opensymphony.xwork2.ActionSupport; import com.frank.rule.User; public class LoginAction extends ActionSupport { private User user; @Override public String execute() throws Exception { if(user.login()){ return SUCCESS; } return INPUT; } @Override public void validate() { if(user.getUsername()==null||user.getUsername().trim().length()<1){ addFieldError("no.username","must input username"); } if(user.getPassword()==null||user.getPassword().trim().length()<1){ addFieldError("no.password","must intpu password"); } } public User getUser() { return user; } public void setUser(User user) { pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run this.user = user; } } 在这里页面的数据不再由控制器的username和password数据成员获取,转而有其中的user对象进行封装。所以应该修改login.jsp的提交表单,修改后的结果如下:
Username:
Password:
注意此时提交表单中的文本框名称必须为user.username和user.password,这样所输入的内容会由控制器的user对象获取。 这里可能有一个疑问,就是在控制器中并没有在任何时候创建User对象,那么内容是如何获得的呢,难道不会抛出NullPointerException,此时当请求控制器,并设置user对象的username数据成员时,action依次调用下方法: user.getUser(); user.setUser(new User()); user.getUser().setUsername("admin"); 从上可以看出,首先struts2会尝试获取user对象,如果不存在则执行第二步创建一个新对象,然后将内赋给数据成员 重新部署,运行,所得到的效果和前一个相同。 使用Servlet相关对象 在进行Web编程时,很多时候需要使用Servlet相关对象,例如:HttpServletRequest、HttpServletResponse、HttpSession、ServletContext。我们可以将一些信息存放到session中,然后在需要的时候取出。 pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run 在struts1.x的Action中,可以通过HttpServletRequest和HttpServletResponse参 数来得到Servlet相关对象,进而使用。那么在struts2种如何获取, 我们可以使用com.opensymphony.xwork2.ActionContext类来完成上述操作。此类的getContext方法可以得到当前Action的上下文,也就是当前Action所处的容器环境,进而得到相关对象。 同时也可以使用org.apache.struts2.ServletActionContext类获得,例如: HttpServletRequest req=ServletActionContext.getRequest(); 下面更改前面的登录操作,在登录成功后,将登录人员的信息保存到session范围中,在 index.jsp页面中取出,我们可以重构我们的Action,在重构前,首先新增一个BaseAction,作为所有Action的父类,在其中增加方法获取Servlet相关对象,这样所有的子类都拥有了相应的方法。代码如下: package com.frank.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; public class BaseAction extends ActionSupport { public HttpServletRequest request(){ return ServletActionContext.getRequest(); } public HttpServletResponse response(){ return ServletActionContext.getResponse(); } public ServletContext application(){ return ServletActionContext.getServletContext(); } public HttpSession session(){ return ServletActionContext.getRequest().getSession(); pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run } } 然后重构LoginAction,继承自BaseAction,并在登录成功后再session范围保存用户名信息,以便index.jsp读取,重构后代码如下: package com.frank.action; import com.opensymphony.xwork2.ActionSupport; import com.frank.rule.User; public class LoginAction extends BaseAction { private User user; @Override public String execute() throws Exception { if(user.login()){ this.session().setAttribute("username", user.getUsername()); return SUCCESS; } return INPUT; } @Override public void validate() { if(user.getUsername()==null||user.getUsername().trim().length()<1){ addFieldError("no.username","must input username"); } if(user.getPassword()==null||user.getPassword().trim().length()<1){ addFieldError("no.password","must intpu password"); } } public User getUser() { return user; } pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run public void setUser(User user) { this.user = user; } } 在index.jsp中增加读取session中保存内容的代码,如下: <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> My JSP 'index.jsp' starting page

Login Success!!!


Username:<%=session.getAttribute("username") %>

pump running States real-time automatic monitoring and advanced system of energy-saving control, distributed computer control system the system, consists of field sensors and actuators, direct digital control (DDC), communication network, the Central operator station consists of four parts. Building control room located in the medical synthesis building first floor, combined with the fire services communication centre, which consists of Central station, devices such as printers, UPS power. Clean air conditioning for the normal operation of the unit in order to ensure that the operating room, surgical equipment in four layers laminated construction equipment management system monitoring station. Building equipment monitoring system (BAS) of main monitoring content frozen water system monitoring: refrigerator of Kai stopped control; steam pressure detection; according to buildings by needed cold load automatically adjustment cold water unit of run Taiwan number; electric butterfly valve, and cooling pump, and cooling tower, and frozen pump and the cold water unit of order Kai stopped control; according to cooling water of temperature automatically Kai stopped cooling tower fan and the control cooling water for backwater duct bypass valve of open degrees; frozen water of load regulation; expansion tank level control; cooling water frequency fill pump monitored,. (This system by frozen station manufacturers completed frozen water system of monitoring, set a frozen station points station, points station host through communications conversion unit put about data to building equipment management system host) clean air conditioning unit monitoring (four control): Unit sent wind machine of Kai/stopped control, and monitored fault, and run
本文档为【Struts2中Action从表单取值并且存到Web元素中&#40;session&#41;】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_624976
暂无简介~
格式:doc
大小:106KB
软件:Word
页数:0
分类:
上传时间:2018-09-18
浏览量:5