Loading...

          java .net

          開始學習struts2(三)

          struts2局部類型轉換

          需求為:在頁面的文本框輸入一個x、y坐標,之間用","隔開,Action中有一個Point類型的屬性,Point類里面有兩個字段,分別是x,y,int型,要求當頁面提交時將文本框中的字符串轉換成Point對象,當Action返回到頁面時將Point轉換成字符串顯示

          1、首先做一個jsp用于輸入和提交:point.jsp

          <s:form action="Converter">
          <s:textfield name="point" label="Point"></s:textfield>
          <s:textfield name="point.x" label="Point.X"></s:textfield>
          <s:textfield name="point.y" label="Point.Y"></s:textfield>
          <s:submit label="submit"></s:submit>
          </s:form>

          2、再建一個用于顯示的jsp-converter.jsp

          <body>
          <s:property value="point"/><br>
          <s:property value="point.x"/><br>
          <s:property value="point.y"/><br>
          </body>

          3、Action類 ConverterAction

          public class ConverterAction extends ActionSupport{

          private Point point;

          public Point getPoint() {
          return point;
          }

          public void setPoint(Point point) {
          this.point = point;
          }

          public String execute()throws Exception {
          return "success";
          }
          }

          4、struts.xml

          <action name="Converter"
          class="com.test.struts2.action.ConverterAction">
          <result name="success">/converter.jsp</result>
          <result name="input">/point.jsp</result>
          </action>

          5、轉換類 ConverterPoint

          public class ConverterPoint extends DefaultTypeConverter {

          @Override
          public Object convertValue(Map context, Object value, Class toType) {
          if(Point.class == toType){
          String[] params = (String[])value;
          params = params[0].split(",");
          Point point = new Point();
          point.setX(Integer.parseInt(params[0]));
          point.setY(Integer.parseInt(params[1]));
          return point;

          }else if(String.class == toType){
          Point point = (Point)value;
          return "x=" + point.getX() + ",y=" + point.getY();
          }
          return super.convertValue(context, value, toType);
          }


          }

          6、配置轉換
          在ConverterAction 類的同一目錄下定義屬性文件ConverterAction-conversion.properties,注意文件名,-conversion.properties為固定不變,ConverterAction為Action的名字,文件內容:

          point=com.test.struts2.action.ConverterPoint

          Ok了,這樣就完工了

          posted on 2009-01-05 13:12 閱讀(292) 評論(0)  編輯  收藏 所屬分類: 學習java

          公告

          希望有一天

          我能用鼠標雙擊我的錢包

          然后選中一張100元

          按住“ctrl+c”

          接著不停的“ctrl+v”

          嘻嘻~~~笑醒~~~



          導航

          <2009年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統計

          常用鏈接

          留言簿(6)

          隨筆分類(102)

          隨筆檔案(398)

          文章分類

          文章檔案(10)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 栖霞市| 河北区| 敦煌市| 左权县| 洛宁县| 新昌县| 扎兰屯市| 仪陇县| 白沙| 南陵县| 平塘县| 临汾市| 广灵县| 徐汇区| 康平县| 齐河县| 阳朔县| 紫金县| 平果县| 宁远县| 虞城县| 齐河县| 额尔古纳市| 孝昌县| 桦南县| 万山特区| 应城市| 永修县| 叶城县| 龙泉市| 城步| 句容市| 正定县| 云霄县| 阿拉尔市| 隆子县| 秦安县| 沧州市| 南皮县| 镇赉县| 灯塔市|