基于用户登陆的struts2中action的分类详解
在struts2中action的分类有:继承 ActionSupport 实现 Action,模型驱动(ModelDriven)的 Action,多方法的 Action三种方式。
1、继承 ActionSupport 实现 Action
通过继承 ActionSupport 来实现 Action 是我们的推荐做法,因为 ActionSupport 中提供了输入验证、国际化、execute 等常用方法,使得编写 Action 时代码很简单。
1.1 UserAction.java
package com.lzugis.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String userpass;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getUserpass()
{
return userpass;
}
public void setUserpass(String userpass)
{
this.userpass = userpass;
}
@Override
public String execute() throws Exception
{
if (username.equals("admin") && userpass.equals("admin"))
{
return "success";
}
else
{
return "error";
}
}
}
1.2 struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 定义包管理配置的action 继承struts-default.xml中的配置 --> <package name="action" extends="struts-default"> <!-- 定义Action(login.action) --> <action name="login" class="com.lzugis.action.UserAction"> <!-- 定义转发路径对应的字符串名 --> <result name="success">/Success.jsp</result> <result name="error">/Error.jsp</result> </action> </package> </struts>
1.3 userlogin.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户登录</title> </head> <body style="font-family:Times New Roman"> <form action="login.action" method="post"> 用户名: <!-- 参数名和action中的属性名一样 --> <input type="text" name="username"><br> 密 码: <input type="password" name="userpass"> <br> <input type="submit" name="subm" value="提交"> <input type="reset" name="reset" value="取消"> </form> </body> </html>
1.4 action响应结果
1.4.1 Success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录成功</title> </head> <body> <h1>欢迎<s:property value="username" />,登录</h1> </body> </html>
1.4.2 Error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登陆错误</title> </head> <body> <h1>用户名或者密码错误</h1> </body> </html>
2、模型驱动(ModelDriven)的 Action
Struts2 的 Action 属于 MVC 模型层, Action 中的方法代表业务逻辑, Action 中的属性代表请求中的参数,当页面请求参数较多的时候,把过多的参数对象的属性定义在 Action 中不太符合 Struts 所倡导的松耦合原则,所以我们推荐单独用 JavaBean 来封装参数,在 Action中为 JavaBean 赋值,这就是 ModelDriven 的 Action。模型驱动的 Action 要求 Action
实现ModelDriven 接口,假如登录页面需要传输参数 username 和 userpass,我们把这 2 个参数封装在一个数据的 JavaBean 中,然后在 Action 中定义该 JavaBean 为 Model 即可。
2.1 UserInfo.java
package com.lzugis.javabean;
public class UserInfo
{
private String username,userpass;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username=username;
}
public String getUserpass()
{
return userpass;
}
public void setUserpass(String userpass)
{
this.userpass=userpass;
}
}
2.2 UserinfoAction.java
package com.lzugis.action;
import com.lzugis.javabean.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserinfoAction extends ActionSupport implements ModelDriven<UserInfo>
{
/**
*
*/
private static final long serialVersionUID = 1L;
private UserInfo model;
@Override
public UserInfo getModel()
{
if(model == null)
{
model = new UserInfo();
}
return model;
}
@Override
public String execute() throws Exception
{
if (model.getUsername().equals("admin") && model.getUserpass().equals("admin"))
{
return "success";
}
else
{
return "error";
}
}
}
2.3 struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 定义包管理配置的action 继承struts-default.xml中的配置 --> <package name="action" extends="struts-default"> <!-- 定义Action(user.action) --> <action name="user" class="com.lzugis.action.UserinfoAction"> <!-- 定义转发路径对应的字符串名 --> <result name="success">/Success.jsp</result> <result name="error">/Error.jsp</result> </action> </package> </struts>
2.4 user.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户登录</title> </head> <body style="font-family:Times New Roman"> <form action="user.action" method="post"> 用户名: <!-- 参数名和action中的属性名一样 --> <input type="text" name="model.username"><br> 密 码: <input type="password" name="model.userpass"> <br> <input type="submit" name="subm" value="提交"> <input type="reset" name="reset" value="取消"> </form> </body> </html>
2.5 action结果
与1相同,在此不在赘述。
本实例通过struts中action的两种不同方式,实现了用户登陆的验证。相比较继承ActionSupport实现action,模型驱动的action比较方便。继承ActionSupport实现action,如果实体类的属性非常多,那么Action中也要定义相同的属性,这样显得比较繁琐。
例子源码:http://download.csdn.net/detail/gisshixisheng/6921547
基于用户登陆的struts2中action的分类详解的更多相关文章
- struts2中struts.xml配置文件详解【未整理】
1. 深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1. 包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管 ...
- Struts2中 Result类型配置详解
一个result代表了一个可能的输出.当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出.在com.opensymphony.xwor ...
- struts2中struts.xml配置文件详解
struts.xml的常用配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...
- struts2中的constant配置详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- Struts2中的拦截器详解
exception:异常拦截器,拦截异常aliasservletConfig18nprepare:预备拦截器,这个拦截器就是为了ModelDriven准备对象的,若Action类实现了preparab ...
- Struts2中Action接收参数的方法主要有以下三种:
Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数(最原始的方式): a.定义:在Action类中定义属性,创建get和set方法: b.接 ...
- Struts2中Action取得表单数据的几种方法
Struts2中Action取得表单数据的几种方法 Struts2中Action获得表单数据的几种方法struts2 Action获取表单传值 1.通过属性驱动式JSP: <form act ...
- 关于struts2中action请求会执行两次的问题
关于struts2中action请求会执行两次的问题 在struts2中发现,调用action中的方法,方法会被执行两次,后来发现调用的方法是get开头的,把它改为其他名称开头的后,就不会执行 ...
- struts2中Action到底是什么,怎么理解
struts2中Action到底是什么,怎么理解 1.配置完web.xml2.创建视图页面login.jsp3.创建业务控制器LoginAction类(解释说:创建业务控制器LoginAction类, ...
随机推荐
- 主机名 域名 网站名 URL
举几个域名的例子:google.com,baidu.com,163.com可以明确的告诉你,加上www,就不再是域名了! 以http://mail.163.com/index.html为例进行说明:1 ...
- 我们为什么使用ORM
我们为什么使用ORM? http://www.cnblogs.com/tansm/archive/2006/06/07/419927.html 博客园在推广ORM方面的确做了很大的贡献,很多的程序员开 ...
- (转)牛B的代码不一定是好代码
最近经常做业务逻辑代码review的工作,发现各种风格的代码,其中有一种是封装和抽象做的非常的多,代码层次非常的深入,表面给人感觉是:牛逼的代码. 但是从清晰度和可维护性来说,还是不推荐这么做. 1. ...
- 29最小的K个数
题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 利用快速排序的partion 来解决 如果基于数字的第 ...
- module.exports和exports得区别
对module.exports和exports的一些理解 可能是有史以来最简单通俗易懂的有关Module.exports和exports区别的文章了. exports = module.exports ...
- java springboot整合zookeeper入门教程(增删改查)
java springboot整合zookeeper增删改查入门教程 zookeeper的安装与集群搭建参考:https://www.cnblogs.com/zwcry/p/10272506.html ...
- 【Java】流与文件(端口 & 文件读写对象)
概述: 1.input和output是相对于内存而言的.输入(input)就是写入到内存里,输出(output)就是把内存里的东西写到外面. 2.操作内存里的东西非常便利,要么声明变量,要么new对象 ...
- Bean的id、name、ref、refid
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...
- article嵌套
例如: 为了将每一个部分都突出显示,可以将article添加边框和颜色的属性: <style> article{ border: 1px solid #ff0000; margin: 5p ...
- MATLAB安装libsvm工具箱的方法
支持向量机(support vector machine,SVM)是机器学习中一种流行的学习算法,在分类与回归分析中发挥着重要作用.基于SVM算法开发的工具箱有很多种,下面我们要安装的是十分受欢迎的l ...