[求助]如何使用DynaAction和DynaActionForm

本人开发一论坛系统,为减少代码量,想把所有关于一张表的操作放在一个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"
[color=red]input="/login.jsp"[/color]<--就是这里,如果有多个页面要用到这个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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值