前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于用户登陆的struts2中action的分类详解

基于用户登陆的struts2中action的分类详解

作者头像
lzugis
发布2018-10-23 15:54:31
3780
发布2018-10-23 15:54:31
举报

在struts2中action的分类有:继承 ActionSupport 实现 Action,模型驱动(ModelDriven)的 Action,多方法的 Action三种方式。

1、继承 ActionSupport 实现 Action

通过继承 ActionSupport 来实现 Action 是我们的推荐做法,因为 ActionSupport 中提供了输入验证、国际化、execute 等常用方法,使得编写 Action 时代码很简单。

1.1 UserAction.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
<%@ 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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<%@ 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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年02月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、继承 ActionSupport 实现 Action
    • 1.1 UserAction.java
      • 1.2 struts.xml
        • 1.3 userlogin.jsp
          • 1.4 action响应结果
            • 1.4.1 Success.jsp
            • 1.4.2  Error.jsp
        • 2、模型驱动(ModelDriven)的 Action
          • 2.1 UserInfo.java
            • 2.2 UserinfoAction.java
              • 2.3 struts.xml
                • 2.4 user.jsp
                  • 2.5 action结果
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档