/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.sunsoft.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.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.actions.DispatchAction; /** * MyEclipse Struts * Creation date: 12-15-2007 * * XDoclet definition: * @struts.action path="/insert" name="userForm" input="/adduser.jsp" scope="request" validate="true" * @struts.action-forward name="prepare" path="/adduser.jsp" * @struts.action-forward name="success" path="success.jsp" */ public class InsertAction extends DispatchAction { /* * Generated Methods */ /** * Method execute * @param mapping * @param form * @param request * @param response * @return ActionForward */ public ActionForward prepare(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { this.saveToken(request); return mapping.findForward("prepare"); } public ActionForward adduser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { if(!this.isTokenValid(request)){ ActionMessages messages = new ActionMessages(); messages.add("token", new ActionMessage("error.token")); this.saveErrors(request, messages); return mapping.getInputForward(); }else{ this.resetToken(request); return mapping.findForward("success"); } } }
页面文件:
adduser.jsp
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <html> <head> <title>JSP for UserForm form</title> </head> <body> <html:form action="/insert?method=adduser"> sex : <html:text property="sex"/><html:errors property="sex"/><br/> userName : <html:text property="userName"/><html:errors property="userName"/><br/> password : <html:password property="password"/><html:errors property="password"/><br/> <html:submit/><html:cancel/> </html:form> <html:errors/> </body> </html>
配置文件
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="userForm" type="com.sunsoft.struts.form.UserForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action attribute="userForm" input="/adduser.jsp" name="userForm" path="/insert" scope="request" parameter="method" type="com.sunsoft.struts.action.InsertAction"> <forward name="prepare" path="/adduser.jsp" /> <forward name="success" path="/success.jsp" /> </action> </action-mappings> <message-resources parameter="com.sunsoft.struts.ApplicationResources" /> </struts-config>