首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

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

2011-11-20 
新手,有关struts中登陆成功或失败跳转问题大家好,我是一名新手,向各位请教有关struts中登陆成功或失败跳转

新手,有关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(){


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

DataBean代码如下:
package bean;
import java.sql.*;
import bean.UserBean;
public class DataBean{
private Connection conn=null;
private ResultSet rs=null;
private Statement state=null;
String username;
public DataBean(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/house","root","1234");
System.out.println("connection succesful");
}catch(SQLException e){
System.out.println(e.getMessage());
}catch(ClassNotFoundException ex){
System.out.println(ex.getMessage());
}
}


public boolean checkUserLogin(UserBean ub){
boolean result=false;
try{
String sql="select * from user";
state=conn.prepareStatement(sql);
rs=state.executeQuery(sql);
if(rs.equals(ub)){

result=true;

}
}catch(Exception e){
e.printStackTrace();
}
return result;
}

public UserBean getUserBean(UserBean ub){
UserBean userbean=null;
try{
String sql="select * from user";
state=conn.prepareStatement(sql);
rs=state.executeQuery(sql);
if(rs.next()){
userbean=new UserBean();
userbean.setUsername(rs.getString("username"));
userbean.setPassword(rs.getString("password"));
}
}catch(Exception e){
e.printStackTrace();
}
return userbean;
}


public void Close()
{
try{
if(rs!=null){
rs.close();
}
if(state!=null){
rs.close();
}
if(conn!=null){
conn.close();
}
}catch(SQLException e){
e.printStackTrace();
}
System.out.println("close succesful");
}

}

struts-config.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
  <data-sources />
  <form-beans >
  <form-bean name="loginForm" type="house.struts.form.LoginForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
  <action
  attribute="loginForm"
  input="/login.jsp"
  name="loginForm"
  path="/login"
  scope="request"
  type="house.struts.action.LoginAction" >
  <forward name="success" path="/successful.jsp"/>
  <forward name="failed" path="/failed.jsp"/>
  </action>

  </action-mappings>

  <message-resources parameter="house.struts.ApplicationResources" />
</struts-config>
能连上数据库,但是登陆后不管输入正确的帐户还是错误的都跳转到failed页面上去了,我猜可能是DataBean.java这个BEAN没写好,但又不知道该怎么写,各位帮忙看看,提提意见,多谢!


[解决办法]
补充一下,呵呵,我错了:

public boolean checkUserLogin(UserBean ub){
boolean result=false;
int flag=0; 
try{
String sql="select * from user where username= '"+ub.getUsername+" ' and password= '"+ub.getPassword+" '";
state=conn.prepareStatement(sql);
rs=state.executeQuery(sql);
if(rs.next()){ 


flag=rs.getInt(1); //这一句你根据你表中的情况而定啊,我是第一个字段是int型的Uid

rs.close(); 
state.close(); 
if(flag==0) result=false; //这里也是,反正你看一下有没有查询结果,呵呵
else result=true; 
}catch(Exception e){
e.printStackTrace();
}
return result;
}

热点排行