Struts2的Action(二)
Struts2的Action可以是一个POJO(即简单的javaBean),也实现Action接口,或者继承ActionSupport类。
1.Action接口:
public interface Action {
public static final String SUCCESS = "success";
public static final String NONE = "none";
public static final String ERROR = "error";
public static final String INPUT = "input";
public static final String LOGIN = "login";
public String execute() throws Exception;
}
将LoginAction实现Action接口,则execute方法可以改为:
public String execute() {
if(getUserName().equals("kaima") && getPassword().equals("asd")) {
return SUCCESS;
}
else
return ERROR;
}
Action运行所用到的数据都保存在ActionContext中,继续修改我们的LoginAction:
public String execute() {
if(getUserName().equals("kaima") && getPassword().equals("asd")) {
ActionContext.getContext().getSession().put("name", userName);
return SUCCESS;
}
else
return ERROR;
}
上面这句话相当于session.setAttribute("name", userName)。这样,在我们的JSP页面中就能使用name这个值了。
2.ActionSupport类就比较复杂,提供了数据校验功能。
将LoginAction继承ActionSupport类,修改execute并覆盖validate()方法:
public String execute() {
if(getUserName().equals("kaima") && getPassword().equals("asd")) {
return SUCCESS;
}
else
return ERROR;
}
@Override
public void validate() { //validate方法在execute之前执行
if(userName == null || userName.equals(""))
this.addFieldError("username", "用户名不能为空");
if(password == null || password.equals(""))
this.addFieldError("password", "密码不能为空");
}
validate()方法会在execute方法前执行,如果没通过,会将错误信息存放到Action的fieldError中,并将请求转发到input对应的逻辑视图中。在struts.xml中指定input对应的视图:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="myStruct" extends="struts-default">
<action name="login" class="myWeb.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
如果想将错误信息显示出来,需要在login.jsp中加上:
<%@ taglib uri="/struts-tags" prefix="s" %>
并在你想显示错误信息的地方加上:
<s:fielderror></s:fielderror> <form action="login.action" method="post"> <table>......
这里我是在table前加上错误信息的显示,如果用户不输入用户名/密码,就会如下:
Struts2的Action(二)的更多相关文章
- Struts2(五)Action二配置
一.method参数 action package com.pb.web.action; public class HourseAction { public String add(){ System ...
- Struts2框架学习(二) Action
Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...
- Struts2基础学习(二)—Action
一.ActionSupport 为了让用户开发的Action类更加规范,Struts2提供了一个Action接口,这个接口定义了Struts2的Action处理类应该实现的规范.下面是标准A ...
- Struts2学习(二)运行Action中方法的三种方式
1.运行execute()方法 一般的能够直接在action中书写execute,调用action时会自己主动运行此方法 2.配置method方法 在struts.xml中配置action时.写met ...
- struts2的action是多例,servlet是单例
struts2中action是多例的,即一个session产生一个action如果是单例的话,若出现两个用户都修改一个对象的属性值,则会因为用户修改时间不同,两个用户访问得到的 属性不一样,操作得出的 ...
- Struts2之Action
Struts2之Action MVC模式中需要有一个控制器来负责浏览器与服务器之间的通信,实现用户与服务器的交互.在Struts2框架中实现这一功能的是Action,它是整个框架最核心的部分.Acti ...
- struts2 修改action的后缀
struts2 修改action的后缀 struts2 的默认后缀是 .action 虽然很直观,但是很烦琐.很多人喜欢将请求的后缀改为 .do 在struts2中修改action后缀有两种比较简单的 ...
- struts2中Action到底是什么,怎么理解
struts2中Action到底是什么,怎么理解 1.配置完web.xml2.创建视图页面login.jsp3.创建业务控制器LoginAction类(解释说:创建业务控制器LoginAction类, ...
- Struts2之Action与配置文件
一.Struts2配置文件 1.struts.properties 在学习Action之前先学下Struts2的配置文件,与Struts2相关的配置文件有好几个,常用的有Struts.xml,web. ...
随机推荐
- ++i与i++的区别
1. ++i 和 i++,在单独使用时,就是 i=i+1. 2. a = ++i,相当于 i=i+1; a = i; (先i = i + 1,再使用i的值).也可以写成 i++; a=i 3. a = ...
- Android定位方式和测试方法
Android常用的三种定位方式有:基于GPS定位.基于基站地位.基于wifi定位. 1.基于GPS定位: GPS定位需要GPS模块(硬件)的支持,没有GPS模块是无法进行GPS定位的. GPS定位最 ...
- Linux crontab 定时任务命令详解
一.简介 crontab 命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于 crontab 文件中,以供之后读取和执行.通常,crontab 储存的指令被守护进程激活, cr ...
- ural 1252. Sorting the Tombstones
1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...
- 【原】iOS学习之卸载Openfire
在即时通信编程中,你的Openfire服务可能因为各种不同的原因,出现不能使用.无法连接等问题. 解决这类问题最直接和省时间的方式就是卸载后重装,本篇主要为大家介绍如何卸载Openfire. 首先,确 ...
- 【原】iOS学习39网络之数据请求
1. HTTP和HTTPS协议 1> URL URL全称是Uniform Resource Locator(统一资源定位符)通过1个URL,能找到互联网上唯一的1个资源 URL就是资源的地址.位 ...
- ACM 变态最大值
变态最大值 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...
- ACM: Mr. Kitayuta's Colorful Graph-并查集-解题报
Mr. Kitayuta's Colorful GraphTime Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- Maven Repository
The usefully link for Maven Reponsitory display as below: http://mvnrepository.com/ For example, To ...
- jQuery取得select选中的值
$("#sxselect").change(function(){ alert($("#sxselect option:selected").val()); } ...