浏览 2786 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-07-08
jsp中 <form id="form1" name="form1" method="post" action="user.do?method=login"> <div align="center"> <label> 用户名 <input type="text" name="username"> </label> <label for="textfield"> 密码 </label> <input type="password" name="password" id="textfield"> <br> </div> <div align="center"> <input type="submit" name="Submit" value="提交" id="Submit"> <a href="register.jsp">注册</a> </div> </form> userAction: /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.yourcompany.struts.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import org.apache.struts.actions.DispatchAction; import com.yourcompany.struts.form.UserForm; import DAO.userDAO; import forusertable.Usertable; import forusertable.UsertableDAO; /** * MyEclipse Struts * Creation date: 07-07-2007 * * XDoclet definition: * @struts.action path="/user" name="userForm" input="/login.jsp" scope="request" validate="true" * @struts.action-forward name="loginsuccess" path="loginsuccess.jsp" * @struts.action-forward name="regsuccess" path="regsuccess.jsp" * @struts.action-forward name="loginfail" path="loginfail.jsp" * @struts.action-forward name="regfail" path="regfail.jsp" */ public class UserAction extends DispatchAction { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { /*DynaActionForm userForm = (DynaActionForm) form;*/// TODO Auto-generated method stub UserForm userForm=(UserForm)form; return null; } public ActionForward login (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ System.out.print("arrive here"); UserForm userForm=(UserForm)form; System.out.print("arrive here"); String username=(String)userForm.getUsername(); String password=(String)userForm.getPassword(); System.out.print("username:"+username+",password:"+password); forusertable.Usertable user=new Usertable(); user.setUsername(username); user.setPassword(password); UsertableDAO userdao=new UsertableDAO(); if(userdao.login(user)){ return mapping.findForward("loginsuccess"); } else{ return mapping.findForward("loginfail"); } } ActionForward tset (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ System.out.print("arrive here tset"); return mapping.findForward("loginsuccess"); } public ActionForward register(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){ DynaActionForm userForm=(DynaActionForm)form; Usertable user=new Usertable(); UsertableDAO userdao=new UsertableDAO(); String username=(String)userForm.get("username"); String password=(String)userForm.get("password"); String realname=(String)userForm.get("realname"); String email=(String)userForm.get("email"); Integer age=(Integer)userForm.get("age"); user.setUsername(username); user.setAge(age); user.setEmail(email); user.setPassword(password); user.setRealname(realname); userdao.save(user); return mapping.findForward("regsuccess"); } } struts-config.xml: <action attribute="userForm" input="/login.jsp"<--就是这里,如果有多个页面要用到这个action应该如何配置啊?--> name="userForm" path="/user" scope="request" type="com.yourcompany.struts.action.UserAction" parameter="method" > <forward name="loginsuccess" path="/loginsuccess.jsp" /> <forward name="regsuccess" path="/regsuccess.jsp" /> <forward name="loginfail" path="/loginfail.jsp" /> <forward name="regfail" path="/regfail.jsp" /> </action> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |