action 类,模拟登陆。
为解决新手struts +json的集成问题,做了一个简单的登陆效果
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
private String username;
private String password;
private String msg;
private String resultCode;
public String execute()
{
System.err.println("loginAction executing....");
if(username==null||password==null)
{
msg = "登陆失败,密码错误";
resultCode = ERROR;
return ERROR;
}
if(username.equals("admin")&&password.equals("admin"))
{
msg = "登陆成功 ";
resultCode = SUCCESS;
return SUCCESS;
}
msg = "登陆失败,密码错误";
resultCode = ERROR;
return ERROR;
}
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 String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
}
struts2 的核心配置文件struts.xml
主页面 index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Index</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<mce:style type="text/css"><!--
#myWindow{padding:10px;display:none;position:absolute;width:400px;height:250px;border:1px solid gray;background: #eee}
--></mce:style><style type="text/css" mce_bogus="1"> #myWindow{padding:10px;display:none;position:absolute;width:400px;height:250px;border:1px solid gray;background: #eee}
</style>
<mce:script type="text/javascript" src="jquery-132min2.js" mce_src="jquery-132min2.js"></mce:script>
<mce:script type="text/javascript"><!--
var win ;
var timer ;
$(function(){
win = $("#myWindow");
var w_left = document.body.clientWidth/2 - win.width()/2;
var w_top = document.body.clientHeight/2 - win.height()/2;
win.css({left:w_left+"px",top:w_top+"px"});
$(window).resize(function(){
var w_left = document.body.clientWidth/2 - win.width()/2;
var w_top = document.body.clientHeight/2 - win.height()/2;
win.animate({left:w_left+"px",top:w_top+"px"});
});
$("#loginButton").click(function(){
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
url:"loginAction.action",
data:"username="+username+"&password="+password,
type:"post",
dataType:"json",
success:function(data){
win.html(data.msg);
win.slideDown("slow");
timer = setInterval("autoHide()",2000);
}
})
})
})
function autoHide()
{
win.slideUp("slow");
clearInterval(timer);
}
// --></mce:script>
</head>
<body>
<form action="loginAction.action" method="post">
<table>
<tr>
<td>username</td>
<td><input id="username" type="text" name="username"/></td>
</tr>
<tr>
<td>password</td>
<td><input id="password" type="text" name="password"/></td>
</tr>
<tr>
<td></td>
<td><input type="button" id="loginButton" name="" value="login"/></td>
</tr>
</table>
</form>
<div id="myWindow">
</div>
</body>
</html>
web.xml 不在做详细配置。。。
测试:username:admin password:admin