用户登陆的实现
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class LoginForm extends ActionForm
{
private static final long serialVersionUID = 1L;
private String user = null;
private String password = null;
public String getPassword()
{
return password;
}
public void setPassword( String password )
{
this.password = password;
}
public String getUser()
{
return user;
}
public void setUser( String user )
{
this.user = user;
}
public void reset(ActionMapping mapping,HttpServletRequest request)
{
this.password = null;这里很重要,当用户输入有错时,需要返回登陆界面给用户,为了用户填写方便我们可以设置返回给用户的哪部分信息设置为空
}
}
我用来实现登陆的DispatchAction代码如下:
public ActionForward login( ActionMapping mapping, ActionForm form,
HttpServletRequest req, HttpServletResponse res ) throws Exception
{
Service service = getService();调用业务逻辑
LoginForm loginForm = (LoginForm) form;获取formbean
String user = loginForm.getUser();提取用户名
Person person = service.getUser( user );从业务逻辑中查找用户
ActionMessages messages = new ActionMessages();
ActionMessage am;
if ( person == null )如果用户不存在,我们就返回
{
am = new ActionMessage( "index.jsp.fail.user", user );参数的意义:第一个是主串,而后面的作为arg数组
messages.add( "user", am );把错误信息放到errors 属性为user那里去显示
saveErrors( req, messages );
form.reset( mapping, req );如果出现错误,调用formbean的重置功能
return mapping.findForward( ID.FAIL );
}
if ( !person.getPassword().equals( loginForm.getPassword() ) )如果密码不一致
{
am = new ActionMessage( "index.jsp.fail.password", user );
messages.add( "password", am );
saveErrors( req, messages );
form.reset( mapping, req );
return mapping.findForward( ID.FAIL );
}
setSessionObject( req, person.getType(), person );把用户放到session里
return new ActionForward( person.getType() + ".do", true );我在每个类型用户的类中加入了一个getType来在这里调用,之后动态的去对应的admin.do,student.do,teacher.do的主页面,并且这里实现的不是请求转发,而是请求从定向
}
[1] [2] [3] [4] [5] [6] 下一页