struts登陆问题,望高手们指点啊!(分不多了,就10分了,谢谢大家了!)

hyc_hover 2006-09-01 11:06:53
现有2个用户表A,B。A表中有name,sex,account,password,address字段。A表中有 name,phone,account,password,createdate字段。 要将2个表的字段写入一个 user bean 中,查找account和password进行登陆,在user中查,最后用一个 boolean 判断是 A表还是B表的用户,若是从A中查的 那么user就填充A的信息,若是B 就填充B的信息。

写到 user中

package com.ecg.bean;
import java.io.*;

public class 11user implements Serializable {

/* 私有字段 */
private String name;
private String sex;
private String phone;
private String address;
private String Createdate;

//common params
private String account;
private String password;

//other params
private boolean isEcgUser ;

public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
………………………………………………………………………………
public boolean isEcgUser() {
return isEcgUser;
}
public void setEcgUser(boolean isEcgUser) {
this.isEcgUser = isEcgUser;
}
}


我使用的是struts模式
写了这个user类后 该如何在 loginform和loginaction中实现登陆并且把页面发送到A,B表中用户对应的页面去呢?

public boolean validatePsd(LoginForm forms)
{
String username=CharFilter.filterChar(CharFilter.toChinese(forms.getUsername()));
String psd=CharFilter.filterChar(CharFilter.toChinese(forms.getPassword()));
boolean exist=false;

sql = "select * from ecguser where account='" + username + "' and password='" + psd + "'";
try
{ conn.setAutoCommit(true);
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
//如果存在
if (rs.next())
{
exist=true ;




this.setUser(user) ;
}
}


catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("查找发生错误!");
}
finally
{
try
{
if (stmt!=null)
stmt.close();
if (rs!=null)
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
throw new RuntimeException("运行发生错误!");
}
}

return exist;
}
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}

上面这段程序如何改写一下,才能够实现在A,B表中都进行了查找呢?
...全文
419 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lianhg 2006-09-01
  • 打赏
  • 举报
回复
你这个问题和struts无关,是sql 文的问题,把A和B结合起来查询就可以啦

sql = "select * from (select A.* from A union select B.* from B) where account='" + username + "' and password='" + psd + "'";
lianhg 2006-09-01
  • 打赏
  • 举报
回复
1)public boolean validatePsd(LoginForm forms)
-> public String validatePsd(LoginForm forms) {
String exist="Error";
//如果存在
if (rs.next()) {
this.setUser(user) ;
return rs.get(0);
}
}
2) if ( !sqlDao.validatePsd(forms)) --> if ("Error".equals.(sqlDao.validatePsd(forms)))
hyc_hover 2006-09-01
  • 打赏
  • 举报
回复
谢谢!

1)中
true 提示有错:Type mismatch: cannot convert from boolean to String
return rs.get(0);提示错误:The method get(int) is undefined for the type ResultSet
2)中
错误:Syntax error on token ".", Identifier expected after this token

如何解决啊
lianhg 2006-09-01
  • 打赏
  • 举报
回复
1)public boolean validatePsd(LoginForm forms)
-> public String validatePsd(LoginForm forms) {
String exist="Error";
//如果存在
if (rs.next()) {
exist=true ;
this.setUser(user) ;
return rs.get(0);
}
}

2) if ( !sqlDao.validatePsd(forms)) --> if ( "Error".equals.(sqlDao.validatePsd(forms)) )

3) return mapping.findForward("success"); --> return mapping.findForward(sqlDao.validatePsd(forms));
hyc_hover 2006-09-01
  • 打赏
  • 举报
回复
这个是我原来的LoginDao
public boolean validatePsd(LoginForm forms)
{
String username=CharFilter.filterChar(CharFilter.toChinese(forms.getUsername()));
String psd=CharFilter.filterChar(CharFilter.toChinese(forms.getPassword()));
boolean exist=false;

sql = "select * from ecguser where account='" + username + "' and password='" + psd + "'";
try
{
conn.setAutoCommit(true);
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
//如果存在
if (rs.next())
{
exist=true ;
this.setUser(user) ;
}}


catch(SQLException e)
{
e.printStackTrace();
throw new RuntimeException("查找发生错误!");
}
finally
{
try
{
if (stmt!=null)
stmt.close();
if (rs!=null)
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
throw new RuntimeException("运行发生错误!");
}
}

return exist;
}
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}

原来的LoginAction
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{


LoginForm forms = (LoginForm)form ;

DataSource ds = null ;
Connection conn = null ;

String usrname = forms.getUsername() ;
String password= forms.getPassword() ;

try
{
ds = this.getDataSource(request , "B") ;
conn = ds.getConnection() ;
HttpSession session = request.getSession() ;
LoginDao sqlDao = new LoginDao(conn) ;

ActionMessages errors = new ActionMessages() ;

response.setHeader("Cache-Control", "no-cache") ;

if ( !sqlDao.validatePsd(forms))
{
errors.add(ActionMessages.GLOBAL_MESSAGE , new ActionMessage("error.passwordorname.isvalid")) ;
if ( !errors.isEmpty())
{

}
())) ;
return mapping.findForward("failure") ;
}

session.setAttribute("user", sqlDao.getUser()) ;

return mapping.findForward("success") ;
}
catch(SQLException e)
{
e.printStackTrace() ;
}
finally
{
try
{
if (conn!=null)
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
return mapping.findForward("success");
}

protected void removeFormBean(ActionMapping mapping, HttpServletRequest request)
{
if(mapping.getAttribute() != null)

if("request".equals(mapping.getScope()))
{
request.removeAttribute(mapping.getAttribute());
} else
{
HttpSession session = request.getSession();
session.removeAttribute(mapping.getAttribute());
}
}
}

应该怎么修改一下啊?
hyc_hover 2006-09-01
  • 打赏
  • 举报
回复
不好意思啊 我实在太菜了~~!!没看太明白
再详细点嘛~~~真是非常非常感谢你啊~!
lianhg 2006-09-01
  • 打赏
  • 举报
回复
public boolean validatePsd(LoginForm forms)
-> public String validatePsd(LoginForm forms) {
String exist="Error";
//如果存在
if (rs.next()) {
exist=true ;
this.setUser(user) ;
return rs.get(0);
}
}

if ( !sqlDao.validatePsd(forms)) --> if ( "Error".equals.(sqlDao.validatePsd(forms)) )

return mapping.findForward("success"); --> return mapping.findForward(sqlDao.validatePsd(forms));

hyc_hover 2006-09-01
  • 打赏
  • 举报
回复
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{


LoginForm forms = (LoginForm)form ;

DataSource ds = null ;
Connection conn = null ;

String usrname = forms.getUsername() ;
String password= forms.getPassword() ;

try
{
ds = this.getDataSource(request , "B") ;
conn = ds.getConnection() ;
HttpSession session = request.getSession() ;
LoginDao sqlDao = new LoginDao(conn) ;

ActionMessages errors = new ActionMessages() ;

response.setHeader("Cache-Control", "no-cache") ;

if ( !sqlDao.validatePsd(forms))
{
errors.add(ActionMessages.GLOBAL_MESSAGE , new ActionMessage("error.passwordorname.isvalid")) ;
if ( !errors.isEmpty())
{

}
())) ;
return mapping.findForward("failure") ;
}

session.setAttribute("user", sqlDao.getUser()) ;

return mapping.findForward("success") ;
}
catch(SQLException e)
{
e.printStackTrace() ;
}
finally
{
try
{
if (conn!=null)
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
return mapping.findForward("success");
}

protected void removeFormBean(ActionMapping mapping, HttpServletRequest request)
{
if(mapping.getAttribute() != null)

if("request".equals(mapping.getScope()))
{
request.removeAttribute(mapping.getAttribute());
} else
{
HttpSession session = request.getSession();
session.removeAttribute(mapping.getAttribute());
}
}
}

这个应该怎么改下呢 ,谢谢lianhg(lianhg) 了啊~~!!再帮忙看看
lianhg 2006-09-01
  • 打赏
  • 举报
回复
loginaction {
pbulic AcctionForward execute {
// 判断用户登陆,然后
return mapping.findForward("A"); // 或B 或Error

}

config.xml 中 :
<action>
<forward name = "A" path = "A.jsp" />
<forward name = "B" path = "B.jsp" />

</action>
hyc_hover 2006-09-01
  • 打赏
  • 举报
回复
那下面的程序该如何写呢?
怎么发送到A用户 B用户的页面呢?
iwlk 2006-09-01
  • 打赏
  • 举报
回复
struts==垃圾
lianhg 2006-09-01
  • 打赏
  • 举报
回复
sql = "select * from (select 'A' TableName,A.* from A union select 'B' TableName,B.* from B) where account='" + username + "' and password='" + psd + "'";

TableName 如果是A ,就是A 表。如果是B ,就是B表

hyc_hover 2006-09-01
  • 打赏
  • 举报
回复
啊 但是如何才能知道我是从A/B中读的数据从而使页面转到A/B表中用户的页面呢?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧