当前位置:编程学习 > JAVA >>

求助:SSH+jquery+json做的简单登陆例子,getHibernateTemplate().find异常

学习SSH jquery json做一个简单登陆,过程如下:

table
  user:id name password grade
model
  User:id name password grade
User.hbm.xml:
  //略

UserDaoImpl:
  public User getUserByID(int id) {
return (User)this.getHibernateTemplate().get(User.class,id);
}
  public User getUserByNameAndPass(String name,String pass){
String sql = "from User u where u.name=? and u.password=?";
System.out.println(name+":"+pass);
  List userList = this.getHibernateTemplate().find(sql,new Object[]{name, pass});
  System.out.println(userList.size());
  return userList.size() == 0 ? null : (User) userList.get(0);
}
  //其他方法略

UserServiceImpl:
  public User getUserByID(int id) {
return userDAO.getUserByID(id);
}
  public User getUserByNameAndPass(String name,String pass){
return userDAO.getUserByNameAndPass(name, pass);
}

UserAction:
UserService userService;
private String username;
private String password;
private String grade;
private int result; //0表示登陆失败,1表示登陆成功
public String login() throws Exception{
User user=userService.getUserByNameAndPass(username, password);
if(null==user){
this.setResult(0);
}else{
this.setUsername(user.getName());
this.setPassword(user.getPassword());
this.setGrade(user.getGrade());
this.setResult(1);
}
return SUCCESS;
}

login.jsp:
  function userLoginExe() {
var $username = $('#username');
var $password = $('#password');
if ($username.val() == '') {
msgShow('系统提示', '请输入用户名!', 'warning');
return false;
}
if ($password.val() == '') {
msgShow('系统提示', '请输入密码!', 'warning');
return false;
}
$.ajax({   
  url:'user/loginAction.action',  
  type:'post',
  data: { username: $username.val(), password: $password.val() },
  dataType:'json',  
  success:function(data){
  if(data.result==0){window.alert("登陆失败!")}
  else if(data.result==1){window.alert(data.grade);} 
  }  
  }); 
}
//其他略

struts.xml:
<package name="project" namespace="/user" extends="json-default">
<action name="loginAction" class="cn.frh.action.UserAction" method="login">
<result name="success" type="json">
<param name="includeProperties">  
result,username, password, grade
</param>
</result>
</action>
</package>



我在user表里添加一个name='a' password='b' grade="admin"的记录;
按道理我输入按登陆后会提示登陆失败或者登陆用户的grade(等级);可是,不弹出alert,无报异常;
用于测试,我将loginAction的login的getUserByNameAndPass(name,password)用getUserByID(1)来测试,结果顺利alert(‘admin’),说明配置都是对的;
于是我将断点设置在UserDaoImpl的List userList = this.getHibernateTemplate().find(sql,new Object[]{name, pass})这一句上,单步调试,弹出异常:

Daemon Thread [ajp-8009-CometPoller-7] (Running)
Daemon Thread [http-8888-1] (Suspended)
DefaultActionInvocation.invokeAction(Object, ActionConfig) line: 468
DefaultActionInvocation.invokeActionOnly() line: 291
DefaultActionInvocation.invoke() line: 254
DefaultWorkflowInterceptor.doIntercept(ActionInvocation) line: 176
DefaultWorkflowInterceptor(MethodFilterInterceptor).intercept(ActionInvocation) line: 98
DefaultActionInvocation.invoke() line: 248
AnnotationValidationInterceptor(ValidationInterceptor).doIntercept(ActionInvocation) line: 263
AnnotationValidationInterceptor.doIntercept(ActionInvocation) line: 68
AnnotationValidationInterceptor(MethodFilterInterceptor).intercept(ActionInvocation) line: 98
DefaultActionInvocation.invoke() line: 248
StrutsConversionErrorInterceptor(ConversionErrorInterceptor).intercept(ActionInvocation) line: 138
DefaultActionInvocation.invoke() line: 248
ParametersInterceptor.doIntercept(ActionInvocation) line: 207
ParametersInterceptor(MethodFilterInterceptor).intercept(ActionInvocation) line: 98
DefaultActionInvocation.invoke() line: 248
ActionMappingParametersInteceptor(ParametersInterceptor).doIntercept(ActionInvocation) line: 207
ActionMappingParametersInteceptor(MethodFilterInterceptor).intercept(ActionInvocation) line: 98
DefaultActionInvocation.invoke() line: 248
StaticParametersInterceptor.intercept(ActionInvocation) line: 190
DefaultActionInvocation.invoke() line: 248
MultiselectInterceptor.intercept(ActionInvocation) line: 75
DefaultActionInvocation.invoke() line: 248
CheckboxInterceptor.intercept(ActionInvocation) line: 94
DefaultActionInvocation.invoke() line: 248
FileUploadInterceptor.intercept(ActionInvocation) line: 243
DefaultActionInvocation.invoke() line: 248
ModelDrivenInterceptor.intercept(ActionInvocation) line: 100
DefaultActionInvocation.invoke() line: 248
ScopedModelDrivenInterceptor.intercept(ActionInvocation) line: 141
DefaultActionInvocation.invoke() line: 248
DebuggingInterceptor.intercept(ActionInvocation) line: 270
DefaultActionInvocation.invoke() line: 248
ChainingInterceptor.intercept(ActionInvocation) line: 145
DefaultActionInvocation.invoke() line: 248
PrepareInterceptor.doIntercept(ActionInvocation) line: 171
PrepareInterceptor(MethodFilterInterceptor).intercept(ActionInvocation) line: 98
DefaultActionInvocation.invoke() line: 248
I18nInterceptor.intercept(ActionInvocation) line: 176
DefaultActionInvocation.invoke() line: 248
ServletConfigInterceptor.intercept(ActionInvocation) line: 164
DefaultActionInvocation.invoke() line: 248
AliasInterceptor.intercept(ActionInvocation) line: 190
DefaultActionInvocation.invoke() line: 248
ExceptionMappingInterceptor.intercept(ActionInvocation) line: 187
DefaultActionInvocation.invoke() line: 248
StrutsActionProxy.execute() line: 52
Dispatcher.serviceAction(HttpServletRequest, HttpServletResponse, ServletContext, ActionMapping) line: 498
FilterDispatcher.doFilter(ServletRequest, ServletResponse, FilterChain) line: 434
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 235
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 206
StandardWrapperValve.invoke(Request, Response) line: 233
StandardContextValve.invoke(Request, Response) line: 191
StandardHostValve.invoke(Request, Response) line: 127
ErrorReportValve.invoke(Request, Response) line: 102
StandardEngineValve.invoke(Request, Response) line: 109
CoyoteAdapter.service(Request, Response) line: 298
Http11AprProcessor.process(long) line: 864
Http11AprProtocol$Http11ConnectionHandler.process(long) line: 579
AprEndpoint$Worker.run() line: 1665
Thread.run() line: 619
执行中断。。

似乎错误在hibernatetemplate的find方法上?

百般搜索未果,望高手指点一二 --------------------编程问答-------------------- 将这个方法  public User getUserByNameAndPass(String name,String pass)
改为下面的试下
final String sql = "from User u where u.name='"+name+"' and u.password='"+pass+"'";
List<User> userList = (List<User>) hibernateTemplate.execute(new HibernateCallback(){

@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
return session.createQuery(sql).uniqueResult();
}});
} --------------------编程问答-------------------- 不行,一样的错!~

引用 1 楼 as1136196 的回复:
将这个方法 public User getUserByNameAndPass(String name,String pass)
改为下面的试下
final String sql = "from User u where u.name='"+name+"' and u.password='"+pass+"'";
List<User> userList = (List<User>) hibern……
--------------------编程问答-------------------- 运行到断点处的各变量信息:


this UserDAOImpl  (id=104)
hibernateTemplate HibernateTemplate  (id=157)
logger Log4JLogger  (id=176)
name "a" (id=145)
pass "b" (id=150)
sql "from User u where u.name=? and u.password=?" (id=151)
count 43
hash 0
offset 0
value  (id=155)
ht HibernateTemplate  (id=157)
allowCreate true
alwaysUseNewSession false
beanFactory null
cacheQueries false
checkWriteOperations true
defaultJdbcExceptionTranslator null
entityInterceptor null
exposeNativeSession false
fetchSize 0
filterNames null
flushMode 1
jdbcExceptionTranslator null
logger Log4JLogger  (id=162)
maxResults 0
queryCacheRegion null
sessionFactory $Proxy0  (id=165)
h AbstractSessionFactoryBean$TransactionAwareInvocationHandler  (id=172)
--------------------编程问答-------------------- 不用struts1.x好多年。。。 --------------------编程问答-------------------- 我这是struts2.X,官网新版



引用 4 楼 wokao_wg 的回复:
不用struts1.x好多年。。。
--------------------编程问答--------------------
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,