我理解的一个action类的执行过程是:先 由jsp表单提交后,再调用相应的action类。而在action类中,先类构造函数-->拦截器函数-->execute()执行函数,不知对否?如果上述执行过程正确,下面这个网上经典的例子中,action类的构造函数到底什么时候起作用啊???action类:::package org.apache.struts2.showcase;import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.util.OgnlValueStack;import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.File;
public class UITagExample extends ActionSupport implements Validateable {
    
    private static final long serialVersionUID = -94044809860988047L;        
    String name;
    Date birthday;
    String bio;
    String favoriteColor;
    List friends;
    boolean legalAge;
    String state;
    String region;
    File picture;
    String pictureContentType;
    String pictureFileName;
    String favouriteLanguage;
    String favouriteVehicalType = "MotorcycleKey";
    String favouriteVehicalSpecific = "YamahaKey";
    
    List leftSideCartoonCharacters;
    List rightSideCartoonCharacters;
    
    List favouriteLanguages = new ArrayList();
    List vehicalTypeList = new ArrayList();
    Map vehicalSpecificMap = new HashMap();
    
    String thoughts;
    
    public UITagExample() {
        favouriteLanguages.add(new Language("EnglishKey", "English Language"));
        favouriteLanguages.add(new Language("FrenchKey", "French Language"));
        favouriteLanguages.add(new Language("SpanishKey", "Spanish Language"));
        
        VehicalType car = new VehicalType("CarKey", "Car");
        VehicalType motorcycle = new VehicalType("MotorcycleKey", "Motorcycle");
        vehicalTypeList.add(car);
        vehicalTypeList.add(motorcycle);
        
        List cars = new ArrayList();
        cars.add(new VehicalSpecific("MercedesKey", "Mercedes"));
        cars.add(new VehicalSpecific("HondaKey", "Honda"));
        cars.add(new VehicalSpecific("FordKey", "Ford"));
        
        List motorcycles = new ArrayList();
        motorcycles.add(new VehicalSpecific("SuzukiKey", "Suzuki"));
        motorcycles.add(new VehicalSpecific("YamahaKey", "Yamaha"));
        
        vehicalSpecificMap.put(car, cars);
        vehicalSpecificMap.put(motorcycle, motorcycles);
    }   
        
    public List getLeftSideCartoonCharacters() {
        return leftSideCartoonCharacters;
    }
    public void setLeftSideCartoonCharacters(List leftSideCartoonCharacters) {
        this.leftSideCartoonCharacters = leftSideCartoonCharacters;
    }
        
    public List getRightSideCartoonCharacters() {
        return rightSideCartoonCharacters;
    }
    public void setRightSideCartoonCharacters(List rightSideCartoonCharacters) {
        this.rightSideCartoonCharacters = rightSideCartoonCharacters;
    }
        
    public String getFavouriteVehicalType() {
        return favouriteVehicalType;
    }
    
    public void setFavouriteVehicalType(String favouriteVehicalType) {
        this.favouriteVehicalType = favouriteVehicalType;
    }
    
    public String getFavouriteVehicalSpecific() {
        return favouriteVehicalSpecific;
    }
    
    public void setFavouriteVehicalSpecific(String favouriteVehicalSpecific) {
        this.favouriteVehicalSpecific = favouriteVehicalSpecific;
    }
    
    public List getVehicalTypeList() {
        return vehicalTypeList;
    }
    
    public List getVehicalSpecificList() {
        OgnlValueStack stack = ServletActionContext.getValueStack(ServletActionContext.getRequest());
        Object vehicalType = stack.findValue("top");
        if (vehicalType != null && vehicalType instanceof VehicalType) {
            List l = (List) vehicalSpecificMap.get(vehicalType);
            return l;
        }
        return Collections.EMPTY_LIST;
    }
    
    public List getFavouriteLanguages() {
        return favouriteLanguages;
    }    public String execute() throws Exception {
        return SUCCESS;
    }    /**//* Getters and Setters */
           
    public String doSubmit() {
        return SUCCESS;
    }       
    
    // === inner class 
    public static class Language {
        String description;
        String key;
        
        public Language(String key, String description) {
            this.key = key;
            this.description = description;
        }
        
        public String getKey() { 
            return key; 
        }
        public String getDescription() { 
            return description; 
        }
        
    }    
    
    public static class VehicalType {
        String key;
        String description;
        public VehicalType(String key, String description) {
            this.key = key;
            this.description = description;
        }
        
        public String getKey() { return this.key; }
        public String getDescription() { return this.description; }
        
        public boolean equals(Object obj) {
            if (! (obj instanceof VehicalType)) { 
                return false;
            }
            else {
                return key.equals(((VehicalType)obj).getKey());
            }
        }
        
        public int hashCode() {
            return key.hashCode();
        }
    }    
    
    public static class VehicalSpecific {
        String key; 
        String description;
        public VehicalSpecific(String key, String description) {
            this.key = key;
            this.description = description;
        }
        
        public String getKey() { return this.key; }
        public String getDescription() { return this.description; }
        
        public boolean equals(Object obj) {
            if (! (obj instanceof VehicalSpecific)) {
                return false;
            }
            else {
                return key.equals(((VehicalSpecific)obj).getKey());
            }
        }
        
        public int hashCode() {
            return key.hashCode();
        }
    }
}
           

解决方案 »

  1.   

    表单文件::<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
        <title>UI Tags Example</title>
        <s:head/>
    </head>
    <body><s:actionerror/>
    <s:actionmessage/>
    <s:fielderror /><s:form action="exampleSubmit" method="post" enctype="multipart/form-data" tooltipConfig="#{'jsTooltipEnabled':'true'}">
        <s:textfield 
                label="Name" 
                name="name"
                tooltip="Enter your Name here" />    <s:datepicker
                tooltip="Select Your Birthday"
                label="Birthday"
                name="birthday" />    <s:textarea
                tooltip="Enter your Biography"
                label="Biograph"
                name="bio"
                cols="20"
                rows="3"/>    <s:select
                tooltip="Choose Your Favourite Color"
                label="Favorite Color"
                list="{'Red', 'Blue', 'Green'}"
                name="favoriteColor"
                emptyOption="true"
                headerKey="None"
                headerValue="None"/>    <s:select
                tooltip="Choose Your Favourite Language"
                label="Favourite Language"
                list="favouriteLanguages"
                name="favouriteLanguage"
                listKey="key"
                listValue="description"
                emptyOption="true"
                headerKey="None"
                headerValue="None"/>    <s:checkboxlist
                tooltip="Choose your Friends"
                label="Friends"
                list="{'Patrick', 'Jason', 'Jay', 'Toby', 'Rene'}"
                name="friends"/>    <s:checkbox
                tooltip="Confirmed that your are Over 18"
                label="Age 18+"
                name="legalAge"/>    <s:doubleselect
                tooltip="Choose Your State"
                label="State"
                name="region" list="{'North', 'South'}"
                value="'South'"
                doubleValue="'Florida'"
                doubleList="top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}" 
                doubleName="state"
                headerKey="-1"
                headerValue="---------- Please Select ----------"
                emptyOption="true" />    <s:doubleselect
                tooltip="Choose your Vehical"
                label="Favourite Vehical"
                name="favouriteVehicalType"
                list="vehicalTypeList"
                listKey="key"
                listValue="description"
                value="'MotorcycleKey'"
                doubleValue="'YamahaKey'"
                doubleList="vehicalSpecificList"
                doubleListKey="key"
                doubleListValue="description"
                doubleName="favouriteVehicalSpecific" headerKey="-1"
                headerValue="---------- Please Select ----------"
                emptyOption="true" />    <s:file
                tooltip="Upload Your Picture"
                label="Picture" 
                name="picture" />
                
        <s:optiontransferselect
                tooltip="Select Your Favourite Cartoon Characters"
                label="Favourite Cartoons Characters"
                name="leftSideCartoonCharacters" 
                leftTitle="Left Title"
                rightTitle="Right Title"
                list="{'Popeye', 'He-Man', 'Spiderman'}" 
                multiple="true"
                headerKey="headerKey"
                headerValue="--- Please Select ---"
                emptyOption="true"
                doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}" 
                doubleName="rightSideCartoonCharacters"
                doubleHeaderKey="doubleHeaderKey"
                doubleHeaderValue="--- Please Select ---" 
                doubleEmptyOption="true"
                doubleMultiple="true" />
        
        <s:textarea
                label="Your Thougths"
                name="thoughts" 
                tooltip="Enter your thoughts here" />
                
        <s:submit onclick="alert('aaaa');" />
        <s:reset onclick="alert('bbbb');" />
    </s:form>
        
    </body>
    </html>
    struts.xml代码片段<action name="example" class="org.apache.struts2.showcase.UITagExample">
        <result>example.jsp</result>
        <result name="input">example.jsp</result>
    </action>例6  
      

  2.   

    你真牛逼,把structs源码都整上来了
    流程大体是form的action调struts.xml对应的
    然后看是否有method,没调默认的方法
    最后就跳转页面啦
      

  3.   

    呵呵,我就是想问一下,在action的构造函数中能否对表单的属性进行设置?
    例如表单中有一个这样的选择框:
        <s:select 
                tooltip="Choose Your Favourite Language" 
                label="Favourite Language" 
                list="favouriteLanguages" 
                name="favouriteLanguage" 
                listKey="key" 
                listValue="description" 
                emptyOption="true" 
                headerKey="None" 
                headerValue="None"/>
    那action的构造函数中,这样设置
            favouriteLanguages.add(new Language("EnglishKey", "English Language")); 
            favouriteLanguages.add(new Language("FrenchKey", "French Language")); 
            favouriteLanguages.add(new Language("SpanishKey", "Spanish Language"));
    能对表单选项起作用吗??这是Max On Java Struts 2.0教程中给出的代码
      

  4.   

    后台取数据,前台<s:select>遍历?
      

  5.   

    favouriteLanguages是List对象还是什么?
      

  6.   

    是的,是List对象,我在2.16的appdoc中也找到这个文件了
      

  7.   

    前台的表单能取后台action构造出的数据吗?我比较怀疑,也没有执行出来。
      

  8.   

    而且 s:select本身要求必须有list属性,但list="favouriteLanguages"这样定义也不对啊!表单jsp根本通不过。
    list="{'a','b'}"这样写才能通过
      

  9.   

    struts2里只要set,get,前台就能获取到
      

  10.   

    set不是从表单中获取数据到action中吗?他是拦截器自动调用的函数吧get是从action中取属性变量的值吗?像beans.
      

  11.   

    先汗一下
    private String username;
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    页面${username}就取到值了
      

  12.   

    前台查询表单--》set函数--》action中变量--》get函数--》前台显示结果的表单,应该是这样的流程吧??但从前面的代码中我们看到,action变量中的值 需要直接去作用到 前台查询表单。那这个流程是怎么执行的呢????
      

  13.   

    没懂,本人中文不好,英文更不好
    前台字段名,要和action对上,不知道你是不是这个意思
      

  14.   

    呵呵,先谢谢楼上诸位了!!归根结底的问题是:假设一个提交表单中一个选择框中多个选项,那么在action中能否设置这多个选项的内容?而不是在表单中直接定义选择框中的多个选项。
      

  15.   

    我想${username}或者<s:property value="username" />只是获得选择框中用户提交得那个值而已!
      

  16.   

    因为是先有提交表单,再提交后到action处理啊
      

  17.   

    在action中设置选择框中多个选项的内容,如何把这个设置作用于(调用该action)的提交表单
      

  18.   

    你自己先做个登陆练习吧,就这么问,很难回答你
    你是说输出吧
    循环我一般用foreach
      

  19.   

    谢谢了!login实例我已经做出来了,现在在学习更复杂的标签!看到上面的代码实例,发出的疑问。
      

  20.   

    我知道流程是 form表单提交后,根据struts。xml找到相应的action类去处理提交的表单数据。例如选择框(“英语”,“俄语”,“法语”)中的选定的某一项。但action类代码中对选择框多个选项的设置(例如把选择框内容重新设定为“aa”,"bb","cc")是如何作用于前面提交的表单的??我就想知道执行的思路。
      

  21.   

    你想问下拉框list怎么输出吧
     <s:select                list="%{#session.sub}" //存放在httpSession的list集合                listKey="sname" //下拉列表要中列表框要显示的值                 listValue="sname"//下拉列表的值                key="sname"                   id="subject"                   name="subject" //存放在httpSession的值,主要判断是否被选中时候用的               onchange="c(1)"              theme="simple">
               </s:select>
    <s:select name="" headerKey="-1" headerValue="请选择" list="list" listKey="uId" listValue="uName"/>list是set,get出来的