欧洲s码亚洲m码精品一区,久久精品国产99国产精品澳门,国产精品高清一区二区http://www.aygfsteel.com/produ/category/54579.html<h2>見(jiàn)證學(xué)習(xí)的軌跡,記錄閃光的想法</h2>zh-cnWed, 01 Apr 2015 02:39:37 GMTWed, 01 Apr 2015 02:39:37 GMT60溫故知新:springMVC_02參數(shù)傳遞http://www.aygfsteel.com/produ/articles/424006.html都較瘦都較瘦Tue, 31 Mar 2015 15:56:00 GMThttp://www.aygfsteel.com/produ/articles/424006.htmlhttp://www.aygfsteel.com/produ/comments/424006.htmlhttp://www.aygfsteel.com/produ/articles/424006.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/424006.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/424006.html

都較瘦 2015-03-31 23:56 發(fā)表評(píng)論
]]>
溫故知新:springMVC_01初步http://www.aygfsteel.com/produ/articles/424005.html都較瘦都較瘦Tue, 31 Mar 2015 15:55:00 GMThttp://www.aygfsteel.com/produ/articles/424005.htmlhttp://www.aygfsteel.com/produ/comments/424005.htmlhttp://www.aygfsteel.com/produ/articles/424005.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/424005.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/424005.html1:類庫(kù)的引用,新建一個(gè)maven項(xiàng)目,將springMVC的依賴添加進(jìn)來(lái)
1 <!-- springMVC dependency-->
2 <dependency>
3   <groupId>org.springframework</groupId>
4   <artifactId>spring-webmvc</artifactId>
5   <version>4.0.3.RELEASE</version>
6 </dependency>
2:在web.xml中添加springMVC的控制器
1   <servlet>
2       <servlet-name>demo</servlet-name>
3       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4   </servlet>
5 
6   <servlet-mapping>
7       <servlet-name>demo</servlet-name>
8       <url-pattern>/</url-pattern>
9   </servlet-mapping>
3:添加springMVC的配置文件,注意:文件的名字需要是xxx-servlet,xxx就是剛才web.xml中添加的控制器的名稱
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
 5      xmlns:util="http://www.springframework.org/schema/util"
 6      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 7              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
 8              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              
 9              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
10 
11      <!-- 激活@Controller模式 -->
12      <mvc:annotation-driven />
13      
14      <!-- 對(duì)包中的所有類進(jìn)行掃描,以完成Bean創(chuàng)建和自動(dòng)依賴注入的功能 需要更改 -->
15      <context:component-scan base-package="com.demo.*" />
16  
17       <!-- 默認(rèn)采用BeanNameViewResolver的請(qǐng)求解析方式,即
18           <bean name="/hello.html" class="XXXpackage.XXXclass"/>
19           的方式進(jìn)行請(qǐng)求映射,該配置默認(rèn)開(kāi)啟,不寫亦可,這里采用注解映射
20        -->
21        
22       <!-- 注解方式的請(qǐng)求映射 -->
23      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
24  
25       <!-- 視圖的映射,這里需要根據(jù)controller的返回值來(lái)確定視圖 -->
26      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
27          <property name="prefix">
28              <value>/WEB-INF/jsp/</value>
29          </property>
30          <property name="suffix">
31              <value>.jsp</value>
32          </property>
33      </bean>
34 
35 </beans>
36 
4:添加一個(gè)控制器
 1 package com.duyt.controllor;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 //注明當(dāng)前這個(gè)類是控制器,項(xiàng)目啟動(dòng)時(shí)會(huì)被掃描
 7 @Controller
 8 //該類的請(qǐng)求映射,為"/"
 9 @RequestMapping("/")
10 public class HelloControllor {
11     
12     //該方法的請(qǐng)求映射,需要將類的請(qǐng)求映射和方法的請(qǐng)求映射拼接起來(lái),也就是"/hello"(類的請(qǐng)求映射為"/")
13     @RequestMapping("/hello")
14     public String Hello(){
15         
16         //視圖的響應(yīng),根據(jù)配置文件的配置,會(huì)在WEB-INF/jsp文件夾中查找以hello為名稱,.jsp結(jié)尾的視圖文件
17         return "hello";
18     }
19 }
20 
視圖就省略了,以上就是使用springMVC的流程,這篇就不記錄其他相關(guān)的功能了,傳參,異常,文件上傳等功能后續(xù)再整理,這里就只是記錄一個(gè)最簡(jiǎn)單的體驗(yàn)案例。

都較瘦 2015-03-31 23:55 發(fā)表評(píng)論
]]>
溫故知新:struts2_09其他功能:異常的處理http://www.aygfsteel.com/produ/articles/419438.html都較瘦都較瘦Mon, 03 Nov 2014 02:37:00 GMThttp://www.aygfsteel.com/produ/articles/419438.htmlhttp://www.aygfsteel.com/produ/comments/419438.htmlhttp://www.aygfsteel.com/produ/articles/419438.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419438.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419438.html
Action:
 1 package demo.action;
 2 
 3 public class HelloWorld {
 4 
 5     public String execute() throws Exception {
 6         //模擬一個(gè)異常
 7         if (true) {
 8             throw new Exception("Exception test");
 9         }
10         return "success";
11     }
12     
13 }
14 
struts.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6 
 7     <constant name="struts.devMode" value="true" />
 8     <package name="helloworld" extends="struts-default" namespace="/">
 9 
10         <global-results>
11             <!-- 指定一個(gè)頁(yè)面作為異常頁(yè)面 -->
12             <result name="error">/error.jsp</result>
13         </global-results>
14 
15         <global-exception-mappings>
16             <!-- 配置需要捕獲的異常類型,以及返回結(jié)果 -->
17             <exception-mapping result="error" exception="Exception" />
18         </global-exception-mappings>
19 
20         <action name="hello" class="demo.action.HelloWorld">
21             <result name="success">/helloWorld.jsp</result>
22         </action>
23 
24     </package>
25 
26 </struts> 
全局異常結(jié)果集的配置很簡(jiǎn)便,可以配置一些404或者一些通用的異常頁(yè)面,在頁(yè)面中使用${exception.message}就可以獲得異常信息。

都較瘦 2014-11-03 10:37 發(fā)表評(píng)論
]]>
溫故知新:struts2_08其他功能:表單驗(yàn)證http://www.aygfsteel.com/produ/articles/419427.html都較瘦都較瘦Mon, 03 Nov 2014 01:50:00 GMThttp://www.aygfsteel.com/produ/articles/419427.htmlhttp://www.aygfsteel.com/produ/comments/419427.htmlhttp://www.aygfsteel.com/produ/articles/419427.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419427.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419427.html閱讀全文

都較瘦 2014-11-03 09:50 發(fā)表評(píng)論
]]>
溫故知新:struts2_07其他功能:國(guó)際化http://www.aygfsteel.com/produ/articles/419400.html都較瘦都較瘦Sun, 02 Nov 2014 07:13:00 GMThttp://www.aygfsteel.com/produ/articles/419400.htmlhttp://www.aygfsteel.com/produ/comments/419400.htmlhttp://www.aygfsteel.com/produ/articles/419400.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419400.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419400.html
其實(shí)多語(yǔ)言問(wèn)題,Java本身就提供了解決方案,util工具包中ResourceBundle就可以解決該問(wèn)題,ResourceBundle的說(shuō)明如下寫道
......
This allows you to write programs that can:
  • be easily localized, or translated, into different languages 
  • handle multiple locales at once 
  • be easily modified later to support even more locales
......
顯然,為了能使用國(guó)家化,我們需要為每一個(gè)需要使用的語(yǔ)言單獨(dú)配置他們的語(yǔ)言內(nèi)容。分別新建中文和英文的語(yǔ)言配置文件置于src下
Message_zh.properties
1 username=\u7528\u6237\u540D(用戶名)
2 password=\u5BC6\u7801(密碼)
Message_en.properties
1 username=username
2 password=password
由于properties只支持iso-8859格式的編碼,所以在輸入中文的時(shí)候會(huì)有些問(wèn)題,只是Eclipse自帶的properties編輯器可以進(jìn)行可視化的文本輸入,不需要關(guān)注編碼。另外我們可以通過(guò)Java自帶的轉(zhuǎn)碼工具native2ascii進(jìn)行轉(zhuǎn)碼。
測(cè)試類如下,getBundle方法接收兩個(gè)參數(shù),第一個(gè)是BaseName,可以理解為資源的基礎(chǔ)名,第二個(gè)是語(yǔ)言碼,中文是zh,英文是en等等,測(cè)試類運(yùn)行完畢之后會(huì)給出英文資源文件中username所對(duì)應(yīng)的值,同理Locale設(shè)置為中文則輸出中文的值。所以資源文件我們以Message_en.properties這種格式來(lái)命名,其中en之后還可以添加下劃線跟第三個(gè)值區(qū)域碼,但是一般可以省略。
 1 package demo.i18n;
 2 
 3 import java.util.Locale;
 4 import java.util.ResourceBundle;
 5 
 6 public class I18nTest {
 7 
 8     public static void main(String[] args) {
 9         ResourceBundle rb = ResourceBundle.getBundle("Message", Locale.ENGLISH);
10         System.out.println(rb.getString("username"));
11     }
12     
13 }
14 

基于上面的內(nèi)容,struts2使用它們配置全局國(guó)際化資源,在struts.xml中添加國(guó)家化的配置,其中value就是資源的基礎(chǔ)名
1 <constant name="struts.custom.i18n.resources" value="Message" />
之后,在頁(yè)面上使用struts2的標(biāo)簽進(jìn)行對(duì)應(yīng)語(yǔ)言內(nèi)容的顯示,當(dāng)然,顯示哪種語(yǔ)言需要顯式的將語(yǔ)言碼作為參數(shù)傳入,請(qǐng)求地址需要寫作/XXX.action?request_locale=en
1 全局國(guó)際化<br/>
2 <s:text name="username"/><br/>
除了全局國(guó)際化的配置,struts2還提供了包范圍和action范圍的國(guó)際化,其中包范圍的國(guó)際化,需要將資源文件命名為“package_語(yǔ)言碼.properties”的形式,然后置于需要的包下,struts2會(huì)優(yōu)先取得較小范圍的國(guó)際化資源文件,同時(shí)定義全局國(guó)際化資源和包國(guó)際化資源,那么包范圍的資源會(huì)優(yōu)先生效。至于action范圍的國(guó)際化,因?yàn)閲?guó)際化在日常的開(kāi)發(fā)中本來(lái)用的就不是很多,就算真的碰到需要雙語(yǔ)開(kāi)發(fā)的情況,或許也是做兩套頁(yè)面,或者直接做兩套網(wǎng)站,除非是那種跨國(guó)性質(zhì)的“因特噗啦一吇”,否則不要說(shuō)struts2的全局國(guó)際化的使用,針對(duì)包范圍和action范圍的國(guó)際化,都是少有問(wèn)津。









都較瘦 2014-11-02 15:13 發(fā)表評(píng)論
]]>
溫故知新:struts2_06其他功能:攔截器http://www.aygfsteel.com/produ/articles/419384.html都較瘦都較瘦Sun, 02 Nov 2014 01:28:00 GMThttp://www.aygfsteel.com/produ/articles/419384.htmlhttp://www.aygfsteel.com/produ/comments/419384.htmlhttp://www.aygfsteel.com/produ/articles/419384.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419384.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419384.html struts2的攔截器其實(shí)和過(guò)濾器是一個(gè)概念,雖然自定義的過(guò)濾器用的不多,但是struts2的核心功能都是用攔截器來(lái)實(shí)現(xiàn)的,異常捕獲,參數(shù)綁定,和國(guó)際化等等。

寫一個(gè)最簡(jiǎn)單的過(guò)濾器的例子,感受一下自定義過(guò)濾器的使用就好。
先上action
 1 package demo.action;
 2 
 3 public class InterceptorDemo {
 4     
 5     private String name;
 6     
 7     public String execute(){
 8         System.out.println("我是Execute的方法");
 9         System.out.println("name:" + name);
10         return "success";
11     }
12     
13     //get/set方法略
14     
15 }
16 
創(chuàng)建一個(gè)interceptor只需要新建一個(gè)類,繼承struts2的AbstractInterceptor,重寫intercept方法
 1 package demo.interceptor;
 2 
 3 import com.opensymphony.xwork2.ActionInvocation;
 4 import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 5 
 6 public class InterceptorDemo extends AbstractInterceptor{
 7 
 8     private static final long serialVersionUID = -1739072578539674220L;
 9 
10     @Override
11     public String intercept(ActionInvocation invocation) throws Exception {
12         
13         System.out.println("我是一個(gè)炫酷的攔截器");
14         
15         //校驗(yàn)用戶是否登陸等操作
16         //ServletActionContext.getContext().getSession()...
17         
18         //校驗(yàn)用戶是否有權(quán)限訪問(wèn)等操作
19         //invocation.getProxy().getActionName()...
20         
21         return invocation.invoke();
22     }
23 
24 }
25 
如此一來(lái),攔截器就創(chuàng)建好了,接下來(lái)就是對(duì)攔截器的配置,看到struts.xml
 1    <package name="interceptorDemo" extends="struts-default" namespace="/">
 2    
 3        <!-- 需要在包內(nèi)聲明攔截器,之后可以定義在action中定義對(duì)聲明攔截器的引用 -->
 4        <interceptors>
 5            <interceptor name="InterceptorDemo" class=demo.interceptor.InterceptorDemo"/>
 6        </interceptors>
 7    
 8        <!-- 在指定的action中使用指定的攔截器 -->
 9       <action name="interceptorDemo" 
10             class="demo.action.InterceptorDemo" 
11             method="execute">
12             <interceptor-ref name="InterceptorDemo"/>
13       </action>
14
15    </package>
將定義好的攔截器添加到某個(gè)action的定義中,讓攔截器攔截對(duì)應(yīng)的action,但如果如上述那樣去配置的話,除了自定義攔截器的功能,struts2 的攔截器功能都會(huì)失效,action中的name值根本拿不到。配置中的每個(gè)包都會(huì)繼承struts-default這個(gè)包,簡(jiǎn)單看一下struts-default.xml這個(gè)文件
 1 <interceptor-stack name="defaultStack">
 2   <interceptor-ref name="exception"/>
 3  <interceptor-ref name="alias"/>
 4  <interceptor-ref name="servletConfig"/>
 5  <interceptor-ref name="i18n"/>
 6  <interceptor-ref name="prepare"/>
 7  <interceptor-ref name="chain"/>
 8  <interceptor-ref name="scopedModelDriven"/>
 9  <interceptor-ref name="modelDriven"/>
10  <interceptor-ref name="fileUpload"/>
11  <interceptor-ref name="checkbox"/>
12  <interceptor-ref name="multiselect"/>
                    ...
     <interceptor-ref name="debugging"/>
    </interceptor-stack>
                    ...
    <default-interceptor-ref name="default-stack"/>
                    ...
上述就是部分配置,定義了默認(rèn)的攔截器棧,也就是一組攔截器,從名字就可以看出很多功能都由攔截器完成,所以,自定義的攔截器也需要引用這一組攔截器,需要在配置中添加默認(rèn)攔截器棧
1 <action name="interceptorDemo" 
2             class="org.duyt.action.InterceptorDemo" 
3             method="execute">
4             <interceptor-ref name="InterceptorDemo"/>
5             <interceptor-ref name="defaultStack"/>
6 </action>
或者定義一組攔截器棧
 1        <interceptors>
 2            <interceptor name="InterceptorDemo" class="demo.interceptor.InterceptorDemo"/>
 3         
 4            <!-- 聲明的攔截器棧一定要引用defaultStack,不然struts2無(wú)法發(fā)揮作用,參數(shù)封裝等最基本的功能都會(huì)失效 -->
 5            <interceptor-stack name="selfStack">
 6                <interceptor-ref name="InterceptorDemo"/>
 7                <interceptor-ref name="defaultStack"/>
 8            </interceptor-stack>
 9        </interceptors>
10    
11          <!-- 在指定的action中使用指定的攔截器 -->
12       <action name="interceptorDemo" 
13             class="demo.action.InterceptorDemo" 
14             method="execute">
15             <interceptor-ref name="selfStack"/>
16       </action>
這樣,自定義的攔截器就能融入到完整的功能之中。


都較瘦 2014-11-02 09:28 發(fā)表評(píng)論
]]>
溫故知新:struts2_05其他功能:文件上傳http://www.aygfsteel.com/produ/articles/419380.html都較瘦都較瘦Sat, 01 Nov 2014 14:10:00 GMThttp://www.aygfsteel.com/produ/articles/419380.htmlhttp://www.aygfsteel.com/produ/comments/419380.htmlhttp://www.aygfsteel.com/produ/articles/419380.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419380.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419380.html首先,文件上傳的表單必須是以下設(shè)置
1 <form action="XXX" method="post" enctype="multipart/form-data">
設(shè)置完畢之后,看一下servlet的post的方法的設(shè)置
 1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 2         
 3         request.setCharacterEncoding("utf-8");
 4         
 5         // 首先需要確認(rèn),到底是不是文件上傳的請(qǐng)求,
 6         boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 7         
 8         if (isMultipart) {
 9             // 創(chuàng)建一個(gè)文件處理對(duì)象
10             ServletFileUpload upload = new ServletFileUpload();
11             InputStream is = null;
12             FileOutputStream os = null;
13             try {
14                 // 解析請(qǐng)求中的所有元素
15                 FileItemIterator iter = upload.getItemIterator(request);
16                 while (iter.hasNext()) {
17                     FileItemStream item = iter.next();
18                     is = item.openStream();
19                     //是否是表單域
20                     if (item.isFormField()) {
21                         //其他操作,保存參數(shù)等
22                     } else {
23                         //不是表單域則保存文件
24                         String path = request.getSession().getServletContext().getRealPath("/");
25                         path = path + "/upload/" + item.getName();
26                         os = new FileOutputStream(path);
27                         //流讀寫
28                         byte[] buf = new byte[1024];
29                         while(is.read(buf)>0){
30                             os.write(buf);
31                         }
32                     }
33 
34                 }
35             } catch (FileUploadException e) {
36                 e.printStackTrace();
37             } finally{
38                 if (is != null) {
39                     is.close();
40                 }
41                 if (os != null) {
42                     os.close();
43                 }
44             }
45         }
46     }
洋洋灑灑一大堆,struts2封裝了這些通用的處理,我們可以按照struts2的風(fēng)格來(lái)獲取要上傳文件的對(duì)象,直接寫一個(gè)多文件上傳的例子吧
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>文件上傳</title>
 8 </head>
 9 <body>
10     <form action="FileUpload" method="post" enctype="multipart/form-data">
11         文件上傳測(cè)試:<br>
12         <input type="file" name="text"/><br>
13         <input type="file" name="text"/><br>
14         <input type="file" name="text"/><br>
15         <input type="submit" value="提交">
16     </form>
17 </body>
18 </html>
action為
 1 package demo.fileUpload;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import org.apache.commons.io.FileUtils;
 7 import org.apache.struts2.ServletActionContext;
 8 
 9 public class FileUpload {
10     
11     //文件接收數(shù)組如果是單文件上傳,那就不需要定義數(shù)組了,定義單個(gè)文件對(duì)象就行
12     private File text[];
13     //對(duì)應(yīng)的文件名,這里的文件名是“名字.后綴”的形式,這個(gè)屬性的命名需要是“文件屬性的名字”+FileName。
14     private String[] textFileName;
15     //對(duì)應(yīng)的文件類型,是文件的真實(shí)類型,比如“text/plain”,這個(gè)屬性的命名需要是“文件屬性的名字”+ContentType
16     private String[] textContentType;
17 
18     public String execute() throws IOException{
19         
20         String dir = "";
21         File file = null;
22         
23         for (int i = 0; i < text.length; i++) {
24             //創(chuàng)建要新建的文件位置
25             dir = ServletActionContext.getServletContext().getRealPath("/"+  "/upload/" + textFileName[i];
26             file = new File(dir);
27             //保存文件
28             if (!file.exists()) {
29                 //使用common.io工具包保存文件
30                 FileUtils.copyFile(text[i], file);
31             }
32         }
33         
34         return "success";
35     }
36
37}


都較瘦 2014-11-01 22:10 發(fā)表評(píng)論
]]>
溫故知新:struts2_04常用標(biāo)簽http://www.aygfsteel.com/produ/articles/419345.html都較瘦都較瘦Fri, 31 Oct 2014 09:54:00 GMThttp://www.aygfsteel.com/produ/articles/419345.htmlhttp://www.aygfsteel.com/produ/comments/419345.htmlhttp://www.aygfsteel.com/produ/articles/419345.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419345.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419345.html
首先,為了能夠使用struts2提供的標(biāo)簽,我們需要先添加標(biāo)簽的引用
在JSP頁(yè)面上添加,uri的具體值可以在struts2包的META-INF的struts-tags.tld文件中查看
<%@taglib prefix="s" uri="/struts-tags"%>
先看一下最常用的
1 <s:property value="str" default="" escapeCsv="" escapeHtml="" escapeJavaScript="" escapeXml=""/>
和EL表達(dá)式${str}的作用類似,展示動(dòng)態(tài)數(shù)據(jù),只不過(guò)property標(biāo)簽強(qiáng)化了很多功能,可以免去判斷直接設(shè)定默認(rèn)值,忽略cvs,html等內(nèi)容,雖然struts2的標(biāo)簽不支持EL表達(dá)式但是有一種類似的寫法
1 <s:property value="%{str}"/>

其次是流程控制標(biāo)簽
1 <s:if test="condition != null">
2     <!--你的代碼-->
3 </s:if>
test的內(nèi)容可以使用OGNL表達(dá)式獲取,除了Java那些常規(guī)的判斷寫法,可以將&&可以寫作and,||可以寫作or,和JSTL的判斷類似,都有自己的個(gè)性

之后是迭代標(biāo)簽,迭代標(biāo)簽可以說(shuō)是struts2標(biāo)簽中最常用的標(biāo)簽之一,不僅是因?yàn)榱斜砉δ芎艹R?jiàn),而且迭代標(biāo)簽給出了很好用的功能
假定vals是個(gè)list,那么遍歷這個(gè)list
1 <s:iterator value="vals" var="val" begin="0" end="5" status="st" step="2">
2     <s:property value="val"/>-<s:property value="#st.index"/>-<s:property value="#st.count"/><br>
3 </s:iterator>
begin和end屬性可以靈活的設(shè)定遍歷的區(qū)間,step則是步進(jìn)的長(zhǎng)度,但是step必須在指定了begin之后才生效,特別要說(shuō)明status這個(gè)屬性,聲明了st之后,便可以獲得當(dāng)前遍歷的下標(biāo)或者行號(hào),這樣一來(lái)就可以根據(jù)需求執(zhí)行其他的操作。var屬性則聲明了當(dāng)前的遍歷對(duì)象,使用var屬性之后,會(huì)在ValueStack中的root和ActionContext中各生成一份當(dāng)前對(duì)象,所以也可以寫為<s:property value="#val"/>,如果不聲明var屬性,則只會(huì)在root中生成一份當(dāng)前對(duì)象,是否聲明var屬性,取決于當(dāng)前遍歷的元素類型,比如vals是一個(gè)user列表,那么迭代標(biāo)簽會(huì)把當(dāng)前迭代的user對(duì)象置于棧頂,循環(huán)內(nèi)直接寫<s:property value="name"/>就可以獲取user的name值,無(wú)需聲明var屬性。

假定vals是個(gè)map,那么遍歷這個(gè)map
<s:iterator value="mapVals" var="val" begin="0" end="5" status="st" step="2">
2     <s:property value="mapVals.get(#val.getKey())"/>-<s:property value="#st.index"/>-<s:property value="#st.count"/><br>
</s:iterator>
或者寫為
<s:iterator value="mapVals.keySet()" var="keyId" begin="0" end="5" status="st" step="2">
2     <s:property value="mapVals.get(#keyId)"/>-<s:property value="#st.index"/>-<s:property value="#st.count"/><br>
</s:iterator>

一些其他的表單標(biāo)簽
1 <s:textfield label="username" name="%{user.username}"/>
2 <s:checkboxlist name="name" label="多選框"  list="#{'1':'Nick','2':'lily','3':'Mary' }" listKey="key" listValue="value" value="#{'1','2'}"/>
3 <s:radio label="Conutry" name="Conutry" value="2" list="#{'1':'中國(guó)','2':'美國(guó)','3':'俄羅斯' }" listKey="key" listValue="value"/>
4 <s:select list="users" label="姓名" value="1" listKey="id" listValue="username" headerKey="-1" headerValue="請(qǐng)選擇"/>
這些標(biāo)簽雖然能很好的完成任務(wù),但是不怎么靈活,一般來(lái)說(shuō)前端的這些控件或多或少的都需要的添加自定義的樣式,所以這些標(biāo)簽出現(xiàn)的機(jī)會(huì)比較少,只是簡(jiǎn)單記錄一下。
還有很多其他的標(biāo)簽,比如form等,HTML自帶標(biāo)簽完全可以滿足一般需求。
這里特別提一下兩個(gè)工具類的標(biāo)簽,其中格式化時(shí)間的date標(biāo)簽用的比較頻繁,其次格式化數(shù)字的標(biāo)簽number,可以用來(lái)截取數(shù)字長(zhǎng)度,四舍五入等操作
1 <s:date name="time" format="yyyy-MM-dd"/>
2 <s:number name="money"/>





都較瘦 2014-10-31 17:54 發(fā)表評(píng)論
]]>
溫故知新:struts2_03參數(shù)的傳遞http://www.aygfsteel.com/produ/articles/419290.html都較瘦都較瘦Thu, 30 Oct 2014 08:38:00 GMThttp://www.aygfsteel.com/produ/articles/419290.htmlhttp://www.aygfsteel.com/produ/comments/419290.htmlhttp://www.aygfsteel.com/produ/articles/419290.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419290.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419290.html閱讀全文

都較瘦 2014-10-30 16:38 發(fā)表評(píng)論
]]>
溫故知新:struts2_02請(qǐng)求和處理的映射http://www.aygfsteel.com/produ/articles/419191.html都較瘦都較瘦Wed, 29 Oct 2014 16:38:00 GMThttp://www.aygfsteel.com/produ/articles/419191.htmlhttp://www.aygfsteel.com/produ/comments/419191.htmlhttp://www.aygfsteel.com/produ/articles/419191.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419191.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419191.html
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6 
 7    <constant name="struts.action.extension" value="action,," /><!-- 請(qǐng)求后綴名-->  
 8    <package name="helloworld" extends="struts-default" namespace="/"><!-- struts-default這個(gè)包是一定要繼承的,否則struts很多重要功能會(huì)失效-->
 9       <action name="hello" <!-- action的名稱,namespace的值連接/hello表示請(qǐng)求該action  -->
10             class="demo.action.HelloWorld"><!-- class表示該action的位置,在action的配置中,如果不指定調(diào)用哪個(gè)方法,則默認(rèn)調(diào)用execute方法  -->
11             <result name="success">/helloWorld.jsp</result><!-- action的返回結(jié)果,以及相應(yīng)的視圖  -->
12       </action>
13    </package>
14 
15 </struts>
就是一種方式。這個(gè)配置中,就已經(jīng)能很明顯的看出action和url之間的關(guān)系,只是這段配置沒(méi)有指明具體調(diào)用action中的哪個(gè)方法,所有默認(rèn)調(diào)用了execute方法,雖然比較笨拙,但是這可以記為配置映射的第一個(gè)方法。需要特別一提的是struts.action.extension配置,它代表了action的后綴名,逗號(hào)隔開(kāi)。也就是說(shuō),它指定了哪些結(jié)尾的請(qǐng)求為struts請(qǐng)求,默認(rèn)是action和空結(jié)尾的請(qǐng)求。

方法一:
一個(gè)action只對(duì)應(yīng)一個(gè)請(qǐng)求,默認(rèn)調(diào)用execute方法,這樣,為了滿足業(yè)務(wù)需求,package中就會(huì)出現(xiàn)大量的action配置。實(shí)際開(kāi)發(fā)中,除非這個(gè)請(qǐng)求進(jìn)行的操作非常多,以至于書寫的代碼幾千行,甚至上萬(wàn)行,否則一般不會(huì)這么配置,就算真的出現(xiàn)這樣的情況,也極有可能和其他配置方式混搭使用,單獨(dú)使用的比較少。
優(yōu)點(diǎn):配置簡(jiǎn)單明了,而且action中只有一個(gè)方法,方便閱讀
缺點(diǎn):會(huì)出現(xiàn)大量的action配置,大量的action類

方法二:
為了讓一個(gè)action能夠?qū)?yīng)多個(gè)請(qǐng)求,可以在方法一的基礎(chǔ)上為action的配置添加method屬性
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6 
 7    <package name="helloworld" extends="struts-default" namespace="/">
 8       <action name="hello" method="method01"
 9             class="demo.action.HelloWorld">
10             <result name="success">/helloWorld.jsp</result>
11       </action>
12    </package>
13 
14 </struts>
如此一來(lái),action中的method01方法就會(huì)對(duì)應(yīng)/hello請(qǐng)求。
優(yōu)點(diǎn):可以減少action類的數(shù)量
缺點(diǎn):還是有大量的配置?。?br />
方法三:
這種請(qǐng)求和處理的映射有點(diǎn)類似于將方法名當(dāng)做參數(shù)來(lái)傳遞,struts.xml的配置和方法一一致,無(wú)需更改,只是在發(fā)送請(qǐng)求的時(shí)候需要將請(qǐng)求的方法一并發(fā)送,原始的請(qǐng)求是/hello,而連帶方法名的請(qǐng)求是/hello!hello,或者還可以寫/hello?method:hello(假定Helloworld類中有hello方法),這樣一來(lái),就是直接調(diào)用hello方法來(lái)完成處理。
優(yōu)點(diǎn):不僅可以完成一個(gè)action和多個(gè)請(qǐng)求的映射,還能減少action的配置,僅僅只是額外配置action的result即可
缺點(diǎn):地址注意不能寫錯(cuò),實(shí)際開(kāi)發(fā)中不常用,或許只是看起來(lái)請(qǐng)求地址怪怪的。。。但依然是個(gè)不錯(cuò)的配置方式

方法四:
基于通配符的映射
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6 
 7    <package name="helloworld" extends="struts-default" namespace="/">
 8       <action name="*_*" 
 9             class="demo.action.{1}action" method="{2}">
10             <result name="success">/{1}/{2}.jsp</result>
11       </action>
12    </package>
13 
14 </struts>
顯而易見(jiàn),任何形式的“XX_XX”請(qǐng)求都會(huì)得到映射,{1}代表第一個(gè)*的值,{2}代表第二個(gè),以此類推,最主要的是,一定要從action的名稱,到action內(nèi)的方法,直至最后響應(yīng)視圖的位置,都要好好的在請(qǐng)求中寫好?;谕ㄅ浞呐渲梅绞斤@得十分靈活,但也要求十分的細(xì)心,是一種能同時(shí)簡(jiǎn)化action類和配置內(nèi)容的映射方式。這里要特別提到result的結(jié)果,結(jié)果可以是struts提供的SUCCESS,ERROR等但不限于這些返回結(jié)果,自定義的返回結(jié)果只要有對(duì)應(yīng)的相應(yīng)視圖即可。
優(yōu)點(diǎn):配置靈活,精簡(jiǎn)
缺點(diǎn):配置時(shí)要求仔細(xì),使用統(tǒng)一的規(guī)則,所謂的“約定優(yōu)于配置”

實(shí)際開(kāi)發(fā)中,根據(jù)各個(gè)配置方式的優(yōu)缺點(diǎn)自行選擇,配置沒(méi)有絕對(duì)的好壞





都較瘦 2014-10-30 00:38 發(fā)表評(píng)論
]]>
溫故知新:struts2_01整體流程感受http://www.aygfsteel.com/produ/articles/419171.html都較瘦都較瘦Wed, 29 Oct 2014 07:52:00 GMThttp://www.aygfsteel.com/produ/articles/419171.htmlhttp://www.aygfsteel.com/produ/comments/419171.htmlhttp://www.aygfsteel.com/produ/articles/419171.html#Feedback0http://www.aygfsteel.com/produ/comments/commentRss/419171.htmlhttp://www.aygfsteel.com/produ/services/trackbacks/419171.html
環(huán)境如下
系統(tǒng):64位win7


測(cè)試環(huán)境:



先從一個(gè)helloworld的簡(jiǎn)單案例開(kāi)始吧
新建一個(gè)maven項(xiàng)目,選擇


其實(shí)不論使用哪個(gè)框架技術(shù),都無(wú)非三步走,import,config,run,首先,我們import
因?yàn)槭褂胢aven來(lái)管理項(xiàng)目,所以直接添加struts2的依賴到pom.xml
    <dependency>
        
<groupId>org.apache.struts</groupId>
        
<artifactId>struts2-core</artifactId>
        
<version>2.3.7</version>
    </dependency>
之后,我們開(kāi)始config,具體配置哪些呢,首先要讓struts2去過(guò)濾請(qǐng)求,那么肯定要在web.xml中配置struts2的過(guò)濾器,然后還需要讓struts2知道請(qǐng)求和控制器之間的關(guān)系,那么肯定還需要再給struts2單獨(dú)進(jìn)行配置。
首先添加struts2的過(guò)濾器到web.xml中
    <filter>
      
<filter-name>struts2</filter-name>
      
<filter-class>
         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      
</filter-class>
   
</filter>
   
<filter-mapping>
      
<filter-name>struts2</filter-name>
      
<url-pattern>/*</url-pattern>
   </filter-mapping>

其次添加struts2的配置struts.xml,特別說(shuō)一下constant的name值是配置在org.apache.struts.default.properties中的,還有其他常用的常量,比如struts.action.extension=action,,,請(qǐng)求后綴等
配置頭部的dtd在jar包中會(huì)有struts-2.X.dtd的配置,也不用到處搜索了。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd"
>
<struts>

   
<constant name="struts.devMode" value="true" /><!-- 開(kāi)發(fā)模式開(kāi)啟,能夠顯示更詳細(xì)的異常信息-->  
   <package name="helloworld" extends="struts-default" namespace="/"><!-- struts-default這個(gè)包是一定要繼承的,否則struts很多重要功能會(huì)失效-->
      
<action name="hello" <!-- action的名稱,namespace的值連接/hello表示請(qǐng)求該action  -->
            class
="demo.action.HelloWorld"><!-- class表示該action的位置,在action的配置中,如果不指定調(diào)用哪個(gè)方法,則默認(rèn)調(diào)用execute方法  -->
            
<result name="success">/helloWorld.jsp</result><!-- action的返回結(jié)果,以及相應(yīng)的視圖  -->
      
</action>
   
</package>

</struts>

action的內(nèi)容
package demo.action;

public class HelloWorld {
    
public String execute() {
        System.out.println(
"Hello Struts2");
        
return "success";
    }
}

整個(gè)配置的過(guò)程就是這樣了,非常簡(jiǎn)易。


都較瘦 2014-10-29 15:52 發(fā)表評(píng)論
]]>
主站蜘蛛池模板: 江陵县| 江孜县| 晋州市| 河池市| 天门市| 曲周县| 军事| 英吉沙县| 沂源县| 台前县| 嘉兴市| 博爱县| 安福县| 钟祥市| 丰顺县| 泾阳县| 琼海市| 遵义县| 通城县| 安图县| 彭水| 砚山县| 合肥市| 子长县| 武鸣县| 丰原市| 荆门市| 乐都县| 信阳市| 保德县| 泸溪县| 文安县| 玉林市| 伽师县| 泰兴市| 阳信县| 隆安县| 江门市| 炉霍县| 富顺县| 广东省|