论坛首页 入门技术论坛

[求助]如何使用DynaAction和DynaActionForm

浏览 2789 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-07-08  
本人开发一论坛系统,为减少代码量,想把所有关于一张表的操作放在一个DynaAction中.form使用可重用的DynaActionForm.但开发过程一点都不顺利,不知道错在哪里了.请教高人.还有一个DynaAction有多个jsp页面的action指向,在struts-config.xml文件中如何配置.
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>
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics