ssh中dispatchAction+validator验证

情况是这样的 :


public class LoginAction extends DispatchAction {
private UserService us;
private floorService fs;
private houseService hs;
private information infor;
public void setUs(UserService us) {
this.us = us;
}

public void setFs(floorService fs) {
this.fs = fs;
}


public void setHs(houseService hs) {
this.hs = hs;
}


public void setInfor(information infor) {
this.infor = infor;
}

//登录
public ActionForward denglu(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
System.out.print("进入action");
String username=loginForm.getUsername();
String password=loginForm.getPassword();
System.out.println("姓名"+username);
System.out.println("姓名"+password);
System.out.println(us+"us是不是空");
List list=us.findLoginExist(username, password);
if(list!=null && list.size()>0){
User user=(User)list.get(0);
request.getSession().setAttribute("user", user);

return mapping.findForward("success");
}else{
request.setAttribute("msg", "用户名或密码错误");
return mapping.findForward("fail");
}


}
//新用户注册
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("进入用户注册action");
LoginForm loginForm = (LoginForm) form;
String username=loginForm.getUsername();
String password=loginForm.getPassword();
String age=loginForm.getAge();
String sex=loginForm.getSex();
String phone=loginForm.getPhone();
int isadmin=0;
User user=new User();
user.setAge(age);
user.setIsadmin(isadmin);
user.setPassword(password);
user.setUsername(username);
user.setSex(sex);
user.setPhone(Long.parseLong(phone));
us.save(user);
return mapping.findForward("fail");
}
}
这是一个DispatchAction ,里面有2个子action,现在需要用validator验证,验证不通过是分别返回到login.jsp和register.jsp,但这是个DispatchAction ,只有一个input,所有我又把这个DispatchAction 分开定义成2个action,xml中写法如下:
<!-- loginAction -->
  //这个是登录用的action
    <action
      attribute="loginForm"
      input="/login.jsp"
     
      name="loginForm"
      parameter="method"
      path="/denglu"
      scope="request"
      validate="true"
      type="com.bsw.struts.action.LoginAction">
      <forward name="select" path="/changeInfor.jsp" />
      <forward name="success" path="/main.jsp" />
      <forward name="fail" path="/login.jsp" />
      <forward name="change" path="/changesuccess.jsp" />
      <forward name="showAll" path="/user/showAllFloor.jsp" />
      <forward name="showHouse" path="/user/showHouse.jsp" />
      <forward name="buySuccess" path="/user/buySuccess.jsp" />
      <forward name="havebuy" path="/user/havaBuyHouse.jsp" />
      <forward name="addServe" path="/admin/addSuccess.jsp" />
    </action>
   
    <!-- registerAction -->//注册用的action
    <action path="/register"
   input="/register.jsp"
   attribute="loginForm"
   name="loginForm"
   parameter="method"
   scope="request"
   validate="true"
   type="com.bsw.struts.action.LoginAction"
    >
    <forward name="fail" path="/login.jsp" />
   
   
    </action>

现在已经解决了返回到两个页面的问题,但是更严重的问题出来了

用这个登录的时候<html:form action="/denglu">
<table>
<html:hidden property="method" value="denglu"/>

进入dengluAction中,此处老是报空指针异常:
System.out.println(us+"us是不是空");
List list=us.findLoginExist(username, password);
我输出us果然是空,因为在applicationContext.xml中,loginAction的注入是这样写的
<!-- loginAction的注入 -->
<bean name="/login" class="com.bsw.struts.action.LoginAction">
<property name="us" ref="userService"></property>
<property name="fs" ref="floorService"></property>
<property name="hs" ref="houseService"></property>
<property name="infor" ref="information"></property>
</bean>
,所以现在us并没有初始化,问题追溯到这里不知道该怎么改了,哪位能指点一下迷津啊??这是毕业设计着急要啊,不胜感激!
   

你可能感兴趣的:(xml,jsp,ssh)