当前位置: 代码迷 >> Java Web开发 >> 新手,有关struts中登陆成功或失败跳转有关问题
  详细解决方案

新手,有关struts中登陆成功或失败跳转有关问题

热度:376   发布时间:2016-04-17 12:38:37.0
新手,有关struts中登陆成功或失败跳转问题
大家好,我是一名新手,向各位请教有关struts中登陆成功或失败跳转问题:

login.jsp代码如下:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html> 
<head>
<title>JSP for LoginForm form</title>
</head>
<body>
<html:form action="/login.do">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>

<html:submit/><html:cancel/>
</html:form>
</body>
</html>


LoginForm.java代码如下:

package house.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
import org.apache.struts.action.ActionMessage;


public class LoginForm extends ValidatorForm {

private String password;

private String username;


public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors=new ActionErrors();
if(this.getUsername()==null||this.getUsername().length()<1){
errors.add("nameerr", new ActionMessage("err.user.login.namenullerr"));
}
if(this.getPassword()==null||this.getPassword().length()<1){
errors.add("passworderr", new ActionMessage("err.user.login.passwordnullerr"));
}

return errors;
}


public void reset(ActionMapping mapping, HttpServletRequest request) {

}

public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public String getUsername() {
return username;
}


public void setUsername(String username) {
this.username = username;
}
}

LoginAction.java代码如下:

package house.struts.action;

import house.struts.form.LoginForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import bean.UserBean;
import bean.DataBean;

public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
UserBean ub=new UserBean();
DataBean db=new DataBean();

ub.setUsername(loginForm.getUsername());
ub.setPassword(loginForm.getPassword());
if(db.checkUserLogin(ub)){
request.getSession().setAttribute("userbean", db.getUserBean(ub));
return mapping.findForward("success");
}

db.Close();
return mapping.findForward("failed");




}
}

另外定义了两个BEAN,分别为UserBean和DataBean
UserBean代码如下:
package bean;
public class UserBean{
private String username;
private String password;

public String getUsername(){
return username;
}
public void setUsername(String username){
this.username=username;
}

public String getPassword(){
  相关解决方案