Struts 2.1.6 精简实例系列教程(2):用户登录模块的实现
2009-09-23 00:00:00 来源:WEB开发网大家如果想用Struts 2的标签库的话,也可以用Struts 2的Form标签,用之前,要在JSP头配置上Struts 2标签库,换成Struts 2标签的版本后,即:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<s:form action="login">
<s:textfield name="loginName"></s:textfield>
<s:password name="password"></s:password>
<s:submit value="登录"></s:submit>
</s:form>
</body>
</html>
接着,我们写一个登录处理的Action类:loginAction.java,代码如下:
package cn.simple.action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@Results({
@Result(name="success", location="admin/index.action", type="redirect"),
@Result(name="input", location="loginform.jsp", type="dispatcher")
})
public class LoginAction extends ActionSupport {
private String loginName;
private String password;
@Override
public String execute() throws Exception {
return SUCCESS;
}
@Override
public void validate() {
if( ! "simple".equals(loginName)){
addFieldError("loginName", "登录名不正确!");
}else if(! "simple".equals(password)){
addFieldError("password", "密码不正确!");
}
}
//*******Getter和Setter方法***********//
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
更多精彩
赞助商链接