随笔-28  评论-15  文章-81  trackbacks-0
    在页面登录中我们常会用到当用户登录成功后,在页面中显示用户名了信息,下面我们就通过代码进行简单的实现
1. login.jsp
 

<%@ page language="java" pageEncoding="GbK"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<html:html lang="true">

 <head>

    <title>登陆程序</title>

 </head>

 <body>

    <center>

       <h1>登陆程序</h1>

       <hr>

       <br>

       <html:errors/>

       <logic:present name="flag" scope="request">

           <logic:equal name="flag" value="true">

              <h2>登陆成功!!!</h2>

              <h2>欢迎<font color="red"><bean:write name="per" property="name" /></font>光临!!!</h2>

           </logic:equal>

           <logic:equal name="flag" value="false">

              <h2>登陆失败!!!</h2>

              <h2>错误的用户ID或密码!!!</h2>

           </logic:equal>

       </logic:present>

       <br>

       <html:form action="logins.do" method="post">

           用户ID<html:text property="id"/><br>

           &nbsp;&nbsp;码:<html:password property="password"/><br>

           <input type="hidden" name="status" value="login">

           <html:submit value="登陆"/>

           <html:reset value="重置"/>

       </html:form>

    </center>

 </body>

</html:html>

2.Struts中的Action类
 

package com.tanm.struts.action;

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 com.tanm.dao.factory.DAOFactory;

import com.tanm.struts.form.LoginForm;

import com.tanm.vo.Person;

public class LoginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response) {

       LoginForm loginForm = (LoginForm) form;

       // ActionForm中取出要的内容

       Person person = new Person();

       person.setId(loginForm.getId());

       person.setPassword(loginForm.getPassword());

       boolean flag = false;

       flag = DAOFactory.getPersonDAOInstance().isLogin(person);

       request.setAttribute("flag", new Boolean(flag));

       if (flag) {

           // 如果成功,person对象传递出去

           // request.getSession().setAttribute("persons", person);

           request.setAttribute("per", person);    //注意perjsp页面中bean:writeper相对应  
           return mapping.findForward("success");

       } else {

           return mapping.findForward("failure");

       }

    }

}


posted on 2007-10-16 21:51 谭明 阅读(1187) 评论(0)  编辑  收藏 所属分类: Struts

只有注册用户登录后才能发表评论。


网站导航: