java登录中用户类型分类_基于用户登陆的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
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
/Success.jsp
/Error.jsp
1.3 userlogin.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
用戶登錄用戶名:
密??碼:
1.4 action響應結果
1.4.1 Success.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
登錄成功歡迎,登錄
1.4.2 ?Error.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
登陸錯誤用戶名或者密碼錯誤
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
{
/**
*
*/
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
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
/Success.jsp
/Error.jsp
2.4 user.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
用戶登錄用戶名:
密??碼:
2.5 action結果
與1相同,在此不在贅述。
本實例通過struts中action的兩種不同方式,實現了用戶登陸的驗證。相比較繼承ActionSupport實現action,模型驅動的action比較方便。繼承ActionSupport實現action,如果實體類的屬性非常多,那么Action中也要定義相同的屬性,這樣顯得比較繁瑣。
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java登录中用户类型分类_基于用户登陆的struts2中action的分类详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java语言中的数据是如何定义_java
- 下一篇: java加密证书生成_mkcert 1.