struts2的mvc技巧

actionName!functionName.action

 <s:form action="tSlogin!preDate.action" method="post" namespace="/">
                    距離現在的時間<br/><s:textfield name="time" value="20"/>
                    <s:select list="listColl" headerKey="-1" headerValue="--學院--"
                     listKey="id" listValue="college" name="youColl" > </s:select>
                     <s:submit value="查詢"/>
</s:form>

list是一個javaBean的集合 listKey和listValue裏面的值只是一個JavaBean的成員變量,

struts2做控制器獲取前端參數的方法

三種,第一種是form action寫s2的。s2的變量是String 類型的;其餘是前端的事情,感覺s2產生大量的代碼

    第二種是前端寫成成員變量的形式user.name,s2寫成類變量的形式

第三種是implements ModelDriven,getModel()重寫;需要一個javaBean模型,前端寫成String類型;

感覺似乎第三種比較好,符合Java面向對象的設計模式

s2內部還可以多寫執行方法;但是不符合一個控制器對應一個jsp原則;如果一個jsp來回反覆業務;;;

public String prepare() throws Exception
    {
        listTypes=qDao.listType();    
        for (int i = 0; i < listTypes.size(); i++) {
            Type type=listTypes.get(i);
            System.out.println(type.getId()+"--"+type.getName());
        }
        /*Map session=ActionContext.getContext().getSession();
        session.put("listTypes", listTypes);*/    
        return INPUT;
    }

 

public String add() throws Exception
    {
        //添加用戶 Map 
//        Map session1=new HashedMap();
        Map session=ActionContext.getContext().getSession();
        Menber menber=(Menber) session.get("menber");
        Integer numberId=menber.getId();
        //anserCount是默認值 回答的個數,首先是0
        String createTime=new TimeFormat().day();//靜態方法    
        Question question=new Question(createTime,numberId,
          this.getTypeId(), this.getTypeKey(), this.getContent(), 0);
        //調到輸入界面---需要先查詢---意思是再次輸入問題--不是查看和問答問題
        QuestionDao qDao=new QuestionDaoImpl();
        qDao.save(question);
        //應該跳轉到查看界面,不然前端好保存值,
         return this.listQue();
    }

//查看問題列表
    public String listQue() throws Exception
    {
        QuestionDao qDao=new QuestionDaoImpl();
        listQuestion=qDao.listQuestion();
        return SUCCESS;
    }

總之是調試出來的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章