欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

struts2中類(lèi)型轉(zhuǎn)換實(shí)例代碼

 更新時(shí)間:2017年10月19日 10:35:35   作者:黃小魚(yú)ZZZ  
這篇文章主要介紹了struts2中類(lèi)型轉(zhuǎn)換實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。

類(lèi)型轉(zhuǎn)換

        所有的頁(yè)面與控制器傳遞的數(shù)據(jù)都是String類(lèi)型,在對(duì)其進(jìn)行處理時(shí)可能會(huì)用到各種的數(shù)據(jù)類(lèi)型,程序無(wú)法自動(dòng)完成數(shù)據(jù)類(lèi)型的轉(zhuǎn)換,這就需要我們?cè)诖a中進(jìn)行手手動(dòng)操作,這個(gè)過(guò)程就稱為類(lèi)型轉(zhuǎn)換。

內(nèi)置類(lèi)型轉(zhuǎn)換器

     在Web應(yīng)用程序中,用戶在視圖層輸入的數(shù)據(jù)都是字符串,業(yè)務(wù)控制層在處理這些數(shù)據(jù)時(shí),就必須把從視圖層傳遞過(guò)來(lái)的字符串進(jìn)行類(lèi)型轉(zhuǎn)換。Struts2提供了簡(jiǎn)單易用的數(shù)據(jù)類(lèi)型轉(zhuǎn)換機(jī)制,struts2提供的類(lèi)型轉(zhuǎn)換如下:

       1)String:將int、long、double、boolean、String類(lèi)型的數(shù)組對(duì)象轉(zhuǎn)換為字符串
       2)boolean/Boolean:在字符串和布爾值之間進(jìn)行轉(zhuǎn)換
       3)char/Character:在字符串和字符之間進(jìn)行轉(zhuǎn)換
       4)int/Integer,float/Float、long/Long、double/Double:在字符串和數(shù)值類(lèi)型的數(shù)據(jù)之間進(jìn)行轉(zhuǎn)換
       5)Date:在字符串和日期類(lèi)之間進(jìn)行轉(zhuǎn)換。對(duì)于日期類(lèi)型,采用SHORT格式來(lái)處理輸入和輸出,使用當(dāng)前請(qǐng)求關(guān)聯(lián)的Locale來(lái)確定日期格式
      6)數(shù)組類(lèi)型(Array):由于數(shù)組元素本身就有類(lèi)型,struts2使用元素類(lèi)型對(duì)應(yīng)的類(lèi)型轉(zhuǎn)換器,將字符串轉(zhuǎn)換為數(shù)組元素的類(lèi)型,然后再設(shè)置到新的數(shù)組中
      7)Collection、List、Set:struts2會(huì)將用戶提交的字符串?dāng)?shù)據(jù)使用request對(duì)象的getparameterValues(string str)方法,將返回的字符串?dāng)?shù)據(jù)轉(zhuǎn)換成集合類(lèi)型

OGNL表達(dá)式

       Struts2框架支持OGNL表達(dá)式,通過(guò)OGNL表達(dá)式可以將用戶請(qǐng)求轉(zhuǎn)換為復(fù)合類(lèi)型。

使用類(lèi)型轉(zhuǎn)換注解

Struts2提供了一些類(lèi)型轉(zhuǎn)換注解來(lái)配置轉(zhuǎn)換器,使得能夠代替ClassName-conversion.properties文件,其中包括以下注解:

       1)TypeConversion注解。該注解應(yīng)用于屬性和方法級(jí)別。
       2)Conversion注解。Conversion注解讓類(lèi)型轉(zhuǎn)換應(yīng)用到類(lèi)型級(jí)別,即可以應(yīng)用到類(lèi)、接口或枚舉聲明。該注解只有一個(gè)參數(shù)conversions。
       3)Element注解。Element注解用于指定Collection或Map中的元素類(lèi)型,該注解只能用于字段或方法級(jí)別。
       4)Key注解。Key注解用于指定Map中的Key的類(lèi)型,該注解只能用于字段或方法級(jí)別。
       5)KeyProperty注解。Keyproperty注解指定用于索引集合元素中的屬性名,該注解只適用于字段或方法級(jí)別
       6)CreatelfNull注解。CreateifNull注解指定在引用的集合元素為null時(shí),是否讓框架重新創(chuàng)建該集合元素。該注解只適用于字段或方法級(jí)別

一個(gè)簡(jiǎn)單的添加商品信息的實(shí)例:
在配置好Struts2環(huán)境后,
  商品類(lèi):

package com.mxl.entity; 
public class Product { 
    private String name;//商品名稱 
    private double price;//商品價(jià)格 
    private int num;//入庫(kù)數(shù)量 
    private String content;//商品描述 
  public String getName() { 
    return name; 
  } 
  public void setName(String name) { 
    this.name = name; 
  } 
  public double getPrice() { 
    return price; 
  } 
  public void setPrice(double price) { 
    this.price = price; 
  } 
  public int getNum() { 
    return num; 
  } 
  public void setNum(int num) { 
    this.num = num; 
  } 
  public String getContent() { 
    return content; 
  } 
  public void setContent(String content) { 
    this.content = content; 
  } 
} 

Action:

package com.mxl.actions; 
import com.mxl.entity.Product; 
import com.opensymphony.xwork2.ActionSupport; 
public class ProductAction extends ActionSupport{ 
    private Product product; 
    public Product getProduct() { 
      return product; 
    } 
    public void setProduct(Product product) { 
      this.product = product; 
    } 
    @Override 
    public String execute() throws Exception { 
    return SUCCESS; 
    } 
} 

struts.xml中的配置:

</action> 
   <action name="pro" class="com.mxl.actions.ProductAction"> 
    <result>/pro_success.jsp</result> 
   </action> 

添加成功頁(yè)面:
<%@ taglib prefix="s" uri="/struts-tags" %>

商品名稱:<s:property value="product.name"/><br/><br/> 
 商品價(jià)格:<s:property value="product.price"/><br/><br/> 
 入庫(kù)數(shù)量:<s:property value="product.num"/><br/><br/> 
 商品描述:<s:property value="product.content"/> 

自定義類(lèi)型轉(zhuǎn)換器實(shí)例:

package com.mxl.converter; 
import java.util.Map; 
import org.apache.struts2.util.StrutsTypeConverter; 
import com.mxl.entity.Product; 
public class ProductConverter extends StrutsTypeConverter{ 
  @Override 
  public Object convertFromString(Map context, String[] values, Class toClass) { 
    Product pro = new Product();//實(shí)例化該類(lèi) 
    String[] proValues = values[0].split("/");//將傳遞過(guò)來(lái)的數(shù)組中的第一個(gè)元素以“/”分隔并組成新的數(shù)組 
    pro.setName(proValues[0]);//將新數(shù)組中的第一個(gè)元素賦值給product類(lèi)中name屬性 
    pro.setPrice(doubleValue(proValues[1]));//將新數(shù)組中的第二個(gè)元素賦值給product類(lèi)中price屬性 
    pro.setNum(Integer.parseInt(proValues[2]));//將新數(shù)組中的第三個(gè)元素賦值給product類(lèi)中num屬性 
    pro.setContent(proValues[3]);//將新數(shù)組中的第4個(gè)元素賦值給product類(lèi)中content屬性 
    return pro; 
  } 
  @Override 
  public String convertToString(Map context, Object obj) { 
     Product pro = (Product)obj; 
     return ""; 
  } 
} 

商品類(lèi)使用的是上邊的那個(gè)類(lèi),Action,

package com.mxl.actions; 
import com.mxl.entity.Product; 
import com.opensymphony.xwork2.ActionSupport; 
public class ProConverterAction extends ActionSupport{ 
    private Product product1; 
    private Product product2; 
  public Product getProduct1() { 
    return product1; 
  } 
  public void setProduct1(Product product1) { 
    this.product1 = product1; 
  } 
  public Product getProduct2() { 
    return product2; 
  } 
  public void setProduct2(Product product2) { 
    this.product2 = product2; 
  } 
  @Override 
  public String execute() throws Exception { 
    return SUCCESS; 
  } 
} 

配置:

<action name="proConverter" class="com.mxl.actions.ProConverterAction"> 
    <result>/pro_list.jsp</result> 
 </action> 

添加一個(gè)全局類(lèi)型轉(zhuǎn)換器:

xwork-conversion.properties,
com.mxl.entity.Product=com.mxl.converter.ProductConverter

添加界面:

<font style="font-size:12px; color:red">在文本框中依次輸入商品的名稱、價(jià)格入庫(kù)數(shù)量和描述之間使用“/”分隔</font> 
 <s:form action="proConverter.action" method="post" cssStyle="margin-top:0px;"> 
   <s:textfield name="product1" label="商品1" size="50"/> 
   <s:textfield name="product2" label="商品2" size="50"/> 
   <s:submit value="確認(rèn)入庫(kù)" align="left"/> 
 </s:form> 

添加成功后的跳轉(zhuǎn)界面:

<ul id="heng" class="addPro"> 
    <li style="font-weight:bold;">商品名稱</li> 
    <li style="font-weight:bold;">商品價(jià)格</li> 
    <li style="font-weight:bold;">商品數(shù)量</li> 
    <li style="font-weight:bold;">商品描述</li> 
  </ul> 
  <ul id="heng" class="addPro"> 
    <li><s:property value="product1.name"/></li> 
    <li><s:property value="product1.price"/></li> 
    <li><s:property value="product1.num"/></li> 
    <li><s:property value="product1.content"/></li> 
  </ul> 
  <ul id="heng" class="addPro"> 
    <li><s:property value="product2.name"/></li> 
    <li><s:property value="product2.price"/></li> 
    <li><s:property value="product2.num"/></li> 
    <li><s:property value="product2.content"/></li> 
  </ul> 

復(fù)合類(lèi)型轉(zhuǎn)換異常處理實(shí)例:

User類(lèi),

package com.mxl.entity; 
import java.util.Date; 
public class User { 
    private String username;//用戶名 
    private String password;//密碼 
    private String realname;//真實(shí)姓名 
    private int age;//年齡 
    private Date birthday;//生日 
    private String address;//家庭住址 
  public String getUsername() { 
    return username; 
  } 
  public void setUsername(String username) { 
    this.username = username; 
  } 
  public String getPassword() { 
    return password; 
  } 
  public void setPassword(String password) { 
    this.password = password; 
  } 
  public String getRealname() { 
    return realname; 
  } 
  public void setRealname(String realname) { 
    this.realname = realname; 
  } 
  public int getAge() { 
    return age; 
  } 
  public void setAge(int age) { 
    this.age = age; 
  } 
  public Date getBirthday() { 
    return birthday; 
  } 
  public void setBirthday(Date birthday) { 
    this.birthday = birthday; 
  } 
  public String getAddress() { 
    return address; 
  } 
  public void setAddress(String address) { 
    this.address = address; 
  } 
} 

配置:

<action name="userException" class="com.mxl.actions.UserExceptionAction"> 
   <result>/user_success.jsp</result> 
   <result name="input">/user_regist.jsp</result> 
  </action> 

添加局部資源文件:
User-ExceptionAction.properties,

內(nèi)容:

invalid.fieldvalue.user.age=會(huì)員年齡必須為整數(shù) 
invalid.fieldvalue.user.birthday=會(huì)員出生日期必須為日期格式 

注冊(cè)頁(yè)面Z:

[html] view plain copy print?
<s:form action="userException.action" method="post"> 
   <s:textfield name="user.username" label="用戶名" size="15"/> 
   <s:password name="user.password" label="密碼" size="15"/> 
   <s:textfield name="user.realname" label="姓名" size="15"/> 
   <s:textfield name="user.age" label="年齡" size="15"/> 
   <s:textfield name="user.birthday" label="出生日期" size="15"/> 
   <s:textfield name="user.address" label="家庭住址" size="15"/> 
   <s:submit type="button" value="提交"/> 
  </s:form> 

跳轉(zhuǎn)界面:

用戶名:<s:property value="user.username"/><br/><br/> 
  密碼:<s:property value="user.password"/><br/><br/> 
  真實(shí)姓名:<s:property value="user.realname"/><br/><br/> 
  年齡:<s:property value="user.age"/><br/><br/> 
  出生日期:<s:property value="user.birthday"/><br/><br/> 
  家庭住址:<s:property value="user.address"/><br/><br/> 

總結(jié)

以上就是本文關(guān)于struts2中類(lèi)型轉(zhuǎn)換實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以參閱本站:Struts2 通過(guò)ognl表達(dá)式實(shí)現(xiàn)投影、struts2開(kāi)發(fā)流程及詳細(xì)配置、Struts2修改上傳文件大小限制方法解析等,如有不足之處,歡迎留言指出。下面推薦幾本相關(guān)書(shū)籍下載,供大家學(xué)習(xí)參考。也希望朋友們對(duì)本站多多支持!

一鍵下載,免費(fèi)的哦:

Java虛擬機(jī)規(guī)范(Java SE 8版) PDF

http://www.dbjr.com.cn/books/581230.html

阿里巴巴Java開(kāi)發(fā)手冊(cè)(v1.2.0正式版)pdf版

http://www.dbjr.com.cn/books/575715.html

希望大家能夠喜歡。

相關(guān)文章

  • 詳解Spring框架注解掃描開(kāi)啟之配置細(xì)節(jié)

    詳解Spring框架注解掃描開(kāi)啟之配置細(xì)節(jié)

    本篇文章主要介紹了詳解Spring框架注解掃描開(kāi)啟之配置細(xì)節(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • SpringBoot導(dǎo)出Excel的四種實(shí)現(xiàn)方式

    SpringBoot導(dǎo)出Excel的四種實(shí)現(xiàn)方式

    近期接到了一個(gè)小需求,要將系統(tǒng)中的數(shù)據(jù)導(dǎo)出為Excel,且能將Excel數(shù)據(jù)導(dǎo)入到系統(tǒng),對(duì)于大多數(shù)研發(fā)人員來(lái)說(shuō),這算是一個(gè)最基本的操作了,本文就給大家總結(jié)一下SpringBoot導(dǎo)出Excel的四種實(shí)現(xiàn)方式,需要的朋友可以參考下
    2024-01-01
  • 如何使用Playwright對(duì)Java API實(shí)現(xiàn)自動(dòng)視覺(jué)測(cè)試

    如何使用Playwright對(duì)Java API實(shí)現(xiàn)自動(dòng)視覺(jué)測(cè)試

    這篇文章主要介紹了如何使用Playwright對(duì)Java API實(shí)現(xiàn)自動(dòng)視覺(jué)測(cè)試,幫助大家更好的理解和使用Playwright,感興趣的朋友可以了解下
    2021-01-01
  • 如何通過(guò)Java實(shí)現(xiàn)加密、解密Word文檔

    如何通過(guò)Java實(shí)現(xiàn)加密、解密Word文檔

    這篇文章主要介紹了如何通過(guò)Java實(shí)現(xiàn)加密、解密Word文檔,對(duì)一些重要文檔,常需要對(duì)文件進(jìn)行加密,查看文件時(shí),需要正確輸入密碼才能打開(kāi)文件。下面介紹了一種比較簡(jiǎn)單的方法給Word文件加密以及如何給已加密的Word文件解除密碼保護(hù),需要的朋友可以參考下
    2019-07-07
  • SpringBoot整合Web開(kāi)發(fā)之Json數(shù)據(jù)返回的實(shí)現(xiàn)

    SpringBoot整合Web開(kāi)發(fā)之Json數(shù)據(jù)返回的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot整合Web開(kāi)發(fā)其中Json數(shù)據(jù)返回的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • java條件語(yǔ)句示例詳解

    java條件語(yǔ)句示例詳解

    本文給大家介紹java條件語(yǔ)句,Java 中的條件語(yǔ)句允許程序根據(jù)條件的不同執(zhí)行不同的代碼塊,一個(gè) if 語(yǔ)句包含一個(gè)布爾表達(dá)式和一條或多條語(yǔ)句,本文結(jié)合示例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 詳解Netty編碼器和解碼器

    詳解Netty編碼器和解碼器

    很多小伙伴對(duì)Netty編解碼器這方面不是很了解,今天這篇文章給大家詳細(xì)介紹了Netty編碼器和解碼器的相關(guān)知識(shí),需要的朋友可以參考下
    2021-06-06
  • Java 使用keytool創(chuàng)建CA證書(shū)的操作

    Java 使用keytool創(chuàng)建CA證書(shū)的操作

    這篇文章主要介紹了Java 使用keytool創(chuàng)建CA證書(shū)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • springboot+redis+阿里云短信實(shí)現(xiàn)手機(jī)號(hào)登錄功能

    springboot+redis+阿里云短信實(shí)現(xiàn)手機(jī)號(hào)登錄功能

    這篇文章主要介紹了springboot+redis+阿里云短信實(shí)現(xiàn)手機(jī)號(hào)登錄功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • Springboot之@Async不執(zhí)行原因及分析

    Springboot之@Async不執(zhí)行原因及分析

    這篇文章主要介紹了Springboot之@Async不執(zhí)行原因及分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評(píng)論