乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Struts2與Velocity模板

       韋薇薇 2012-02-24

      Velocity是一種基于JAVA的模板引擎,開發(fā)人員使用簡單的模板語言就可以快速開發(fā)顯示層,它使得顯示層與程序代碼分離,在早期的Webwork版本中,所有UI標簽均使用Velocity模板引擎生成,可以在Struts-core-2.0.8.jar中找到template文件夾,其中的archive文件夾下存放著原有的vl文件!

      Struts2中查找vm文件的順序先查找Web應用程序,再查找類路徑。如果你需要使用vm作為顯示層,只需要在配置文件中指定resulttype屬性為velocity,然后將<param>location屬性指定到一個以vm結尾的資源上就可以了!


      1.Struts2中的Velocity中文亂碼的處理方式:

      因為在Velocity-1.4.jar中的org.apache.velocity.runtime.defaults中定義的velocity.properties中定義的input.encoding=ISO8859-1,output.encoding=ISO8859-1,通常最好在VM文件中使用%{getText(‘’)}調(diào)用國際化資源文件的內(nèi)容就不會產(chǎn)生亂碼。

      2.Velocityvm文件必須要經(jīng)過Action才可以訪問,以便被程序解析,不可以直接訪問vm文件。

      3.vm文件中使用Struts2的標簽只需要在原有標簽前加#s就可以了,參數(shù)之間使用””分隔!

      (1.)例如下面是一個表單的定義:

      #sform(“action=Student” namespace=”/ilkj/student”)

             #stextfield(“l(fā)abel=%{getText(‘student.label.name’)}” “name=student.name”)

             #spassword(“l(fā)abel=%{getText(‘student.label.password’)} ” name=student.age”)

             #ssubmit(“value=getText(‘globale.label.submit’)”)

      #end

      (2.)下面是取出session中的一個參數(shù)的值

      #sproperty(“value=#session.information” “default=noValue”)

      (3.)下面是訪問Action中的屬性

      ${student.name},當然你也可以簡單寫成$student.name的形式。



      4.Velocity的內(nèi)建變量:

      stack---ValueStack,可以使用${stack.findString(‘OGNL表達式’)}取值

      action---最近執(zhí)行的Action,例如${action.getStudent().getName()}或者也可以直接寫${action.student.name}

      response---HttpServletRequest

      request---HttpServletResponse

      session---HttpSession

      application---ServletContext

      base---生成應用的上下文,相當與request.getContextPath()



      5.注釋語法

      (1.)單行注釋:##

      (2.)多行注釋:#* ... ... *#


      6.暫停執(zhí)行

      #stop---停止執(zhí)行后面的模板內(nèi)容


      7.轉(zhuǎn)義字符

      Velocity使用 \ 轉(zhuǎn)義字符,例如email被定義為andrew830314@163.com#set($email=’andrew830314@163.com’)),你還想輸出$email而不被Velocity解析,那么你可以使用 \$email。


      8.引入其他文件

      #include(“”),如果是JSP或者vm文件將會顯示內(nèi)容,其他文本文件顯示文本,二進制會顯示亂碼,引入多個文件使用 , 間隔,#parse(“”)引入一個本地的vm文件,與#include(“”)不同,#parse(“”)只能引入一個文件,并且#parse(“”)可以訪問父模板里的變量。


      9.模板代碼復用

      #macro(代碼段名稱 $參數(shù)1 ... ...)

             ... ...

      #end


      只要使用#代碼段名稱($參數(shù)1 ... ...)就可以使用這段代碼,當然你也可以不使用任何參數(shù)


      10.判斷語句

      #if(邏輯表達式)

      注意:在Velocity中使用==判斷兩個變量是否相等,而不是使用equals(“”)方法。


      #else


      #end


      從上面我們還可以總結出,Velocity模板引擎使用#表示一種命令,例如#set是給某個變量設置值,使用$引用變量,使用()表示參數(shù),使用{}表示作用范圍。

      另外需要注意的時候,Velocity只會按照gettXXX()解析變臉變量,例如student.name實際上找到的student.getName()方法,你可以試著在Action寫一個變量而不使用getXXX()方法,看看會不會在vm中取到值,答案是變量會被原樣輸出。

      Velocity使用的都是對象,即使你定義#set ($count=1)也會被包裝成Integer使用。



      另外,修改vm文件需要重新啟動服務器,如果在開發(fā)階段,我們可以設置struts.properties中的struts.devMode=true避免每次重新啟動服務器。


      最后我們思考一下,如果貨幣$2.95會被Velocity引擎直接輸出,還是去查找2.95這個變量?如果你在思考這個問題,說明你的JAVA基礎不過關哦!因為JAVA中的變量不能以數(shù)字開頭的哦!因此你不必擔心貨幣$2.95會被當作變量解析!另外,如果Velocity引擎未找到變量也會原樣輸出,例如${student.name}如果未找到會直接原樣輸出,這也是為什么我們直接訪問JSP的時候會看到大量的${}被原樣輸出。如果變量不存在,你想讓它不輸出,可以這樣寫$!{}




      使用Struts2整合Velocity或者FreeMarker配置如上的Servlet就可以在模板引擎中使用Struts2的標簽了。
      不需要在模板頁面中像JSP那樣引用,前綴都是s在Velocity使用#s...在FreeMarker使用@s. ..。

      Velocity:

      HTML code
      <html> <head> <title>UI Tags Example</title> #shead() #sxhead() </head> <body> #surl ("id=url" "value=index.jsp") <a href="${url}">Back to index.jsp</a>! #sform ("action=exampleSubmitVelocity" "method=post" "enctype=multipart/form-data") #stextfield ("label=Name" "name=name") #sxdatetimepicker ("label=Birthday" "name=birthday") #sxdatetimepicker ("label=Wake up time" "name=wakeup" "type=time") #stextarea ("label=Biography" "name=bio" "cols=20" "rows=3") #sselect ("label=Favourite Color" "list={'Red', 'Blue', 'Green'}" "name=favouriteColor" "emptyOption=true" "headerKey=None" "headerValue=None") #sselect ("label=Favourite Language" "list=favouriteLanguages" "name=favouriteLanguage" "listKey=key" "listValue=description" "emptyOption=true" "headerKey=None" "headerValue=None") #scheckboxlist ("label=Friends" "list={'Patrick', 'Jason', 'Jay', 'Toby', 'Rene'}" "name=friends") #scheckbox ("label=Age 18+" "name=legalAge") #sdoubleselect ("label=State" "name=region" "list={'North', 'South'}" "value='North'" "doubleValue='Florida'" "doubleList=top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}" "doubleName=state" "headerKey=-1" "headerValue=---------- Please Select ----------" "emptyOption=true" ) #sdoubleselect ("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" ) #sfile ("label=Picture" "name=picture") #soptiontransferselect ("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" ) #ssubmit() #sreset() #end #sa("href=${url}")Back to index.jsp#end </body> </html>


      FreeMarker:
      HTML code
      <@s.form action="test"> <@s.textfield label="Name" name="name"/> <@s.select label="Birth Month" headerValue="Select Month" list="months" /> </@s.form>

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多