(.........) 
用来处理数据的action 
UserAction.java 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse;  
import org.apache.struts.action.ActionErrors; 
import org.apache.struts.action.ActionForm; 
import org.apache.struts.action.ActionForward; 
import org.apache.struts.action.ActionMapping; 
import org.apache.struts.actions.DispatchAction;  
public class UserAction  extends DispatchAction 
{ 
    public ActionForward execute(  ActionMapping mapping, 
                                    ActionForm form, 
                                    HttpServletRequest request, 
                                    HttpServletResponse response) 
           throws Exception 
       {  
           //获取页面表单信息 
           UserForm uForm = (UserForm)form; 
           //从from中获得已经被提交的两个数据 
           String temp_str1 = uForm.getStr1(); 
           String temp_str2 = uForm.getStr2(); 
        
           String temp_strAdd = temp_str1+temp_str1; 
           //把处理的结果放到form中去 
uForm.setStrAdd(temp_strAdd); 
           if(!"".equals(temp_str1)&&!"".equals(temp_str2)) 
           { 
              return mapping.findForward("ok"); 
           } 
           else 
           { 
              return mapping.findForward("err"); 
           } 
       } 
} 
赶快运行看看你的结果哦: 
把你的tomcat再次启动一下,在你的ie地址拦输入: 
http://localhost:8080/user/in.jsp 
提示你注意的几个地方 
1在struts-config.xml 中 
<action path="/execute" 
               type="UserAction" 
               name="userForm"…………的execute   UserAction   userForm 
分别和 
in.jsp中<html:form action="execute.do" method="post">的execute.do 
    java类UserAction.class   
struts-config.xml 中<form-bean name="userForm"……….的userForm 
success.jsp中<bean:write name="userForm"…的userForm 
2在struts-config.xml 中 
<form-bean name="userForm"      type="UserForm">的UserForm 
和 
java类UserForm.class 
3在form中看到 
  str1、str2、strAdd了吗? 
  在in.jsp和success.jsp中是都找到了堑挠白恿四兀?/SPAN> 
明白了没有啊? 
哦,原来是这样的。in.jsp把str1、str2的值放到form中去了,然后aciton又利用get方法把他们取出来处理好了后把结果通过set方法再放到form的strAdd中去了,最后呢success.jsp再次把form中的strAdd取出来显示给你看,是这样吧??!! 
就是这样的过程,form成了一个把数据保存(这种保存不是永久的)起来的“仓库”,action成了一个处理并加工数据的“工厂”。他们联合起来提供数据的全套服务给前台的jsp页面了。好了,我要说的已经说完了,关于这个例子,我只有这么多要告诉你。 
哦,忘了,不好意思。为什么当页面的“submit”类型的按钮按下时候,action中的execute方法会被执行呢?哈哈,这就是struts的优点啊!把execute这个方法名该为“myDog”可以吗?现在你还不可以,以后我会告诉你怎么可以的啦。。  
 
  |