Strust小结

来源:互联网 发布:淘宝号被限制了怎么办 编辑:程序博客网 时间:2024/06/05 08:43

Struts2标签:三种给<s:a>,<a>标签传值的方式

1. <s:iterator value="#request.userList">  

2.     <tr align="center">           

3.       <td><s:property value="id"/> : <s:property value="username"/></td>  

4.       <td><s:property value="age"/></td>  

5.       <td><s:property value="sex"/></td>  

6.       <td><s:property value="address"/></td>  

7.       <td>  

8.         <s:a href="userAction!addUser.action">添加</s:a> |   

9.             //第一种方式,在标签内使用标签时用%  

10.             <s:a href="userAction!loadUser.action?user.id=%{id}">编辑</s:a> |         

11.            //第二种方式:使用<s:url>标签解决    

12.         <s:url id="idUrl" action="userAction!delUser.action">  

13.            <s:param name="user.id" value="%{id}"></s:param>  

14.         </s:url>   

15.         <s:a href="%{idUrl}">删除</s:a>  

16.                //第三种:直接加入  

17.         <a href="<s:url action="userAction!delUser.action">  

18.                     <s:param name="user.id" value="id"/>  

19.                  </s:url>">删除2  

20.               </a>          

21.       </td>  

22.     </tr>  

 </s:iterator>


Strust国际化编码

首先在struts.properties文件中加入以下内容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入
<constant name="struts.custom.i18n.resources"value="messageResource"></constant>

资源文件的命名格式: 名称_语言代码_国家代码. Properties
如果创建中文和英语国际化,那么资源文件名称为
messageResource_zh_CN.properties
messageResource_en_US.properties

1. jsp页面的国际化 
通过使用标签<s:textname="label.helloWorld"/>输出国际化
label.helloWorld
为资源文件中定义的key


messageResource_en_US.properties加入以下内容
label.hello=hello {0}
label.helloWorld=hello,world

messageResource_zh_CN.properties加入以下内容
label.hello=
你好{0}
label.helloWorld=
你好,世界

(1).<s:text name="label.helloWorld"/>
<s:property value="%{getText('label.helloWorld')}"/>
上面两个都为输出一个helloword的两种表示 

<s:textfieldname="name" key="label.helloWorld"/>
<s:textfield name="name"label="%{getText('label.helloWorld')}"/>
显示一个文本框,文本框的标题进行国际化 

(2). 使用<s:i18n>标签指定从某个特定的资源文件中取数据
<s:i18n name="messageResource">
   <s:text name="label.helloWorld"></s:text>
</s:i18n>
指定在从messageResource取资源

(3).
<s:text name="label.hello">
   <s:param>callan</s:param>
</s:text>
使用带参数的资源.<s:param>可以替换label.hello=hello{0}中的{0}


上传下载

1、注意:在一个form表单里有选择file的文本框是 form表单一定要添加

<formaction="uploadDemo" method="post" enctype="multipart/form-data">

然后<s:file name="uploadFile" label="浏览..." style="border: 1px solidBlue"/>

提交的时候就没有问题。这个问题纠结死我了。。。

 

2、上传文件的大小设置:在struts.xml里面添加

<constant name="struts.multipart.maxSize"value="104857600"/>

104857600=100M

 

3、action接受参数文件名是固定的。

private   File  uploadFile;// 得到上传的文件,也就是表单里的name值

private   String  uploadFileContentType;//获得文件类型(固定写法)

private   String  uploadFileFileName;//获得要上传的文件名字(固定写法)

4、struts中的<s:submit method>method方法不会执行。

解决方法在struts.xml文件中加入

<constantname="struts.enable.DynamicMethodInvocation"value="true"/>

 

总结:

<constant name="struts.custom.i18n.resources"value="i18n.resource"/>//国际码

 

<constant name="struts.i18n.encoding" value="UTF-8" />//页面中文乱码,同时需要改tomcat中的\apache-tomcat-6.0.41\conf\server.xml

<Connector port="8080" protocol="HTTP/1.1"

              connectionTimeout="20000"

              redirectPort="8443"/>

最后添加上URIEncoding=”UTF-8”

 

<constant name="struts.enable.DynamicMethodInvocation"value="true"/>//struts中的<s>标签中的method可以实现执行

 

<constant name="struts.multipart.maxSize"value="104857600"/>//上传文件最大值。1048576=100M



Jsp中直接一个a标签即可

<h3>下载</h3>

    <s:a href="download">下载Demo</s:a>

Struts.xml中

<action name="download" class="com.allen.demo.action.DownLoad">

    <resultname="success"type="stream">

         <paramname="contentType">application/x-zip-compressed</param>

         <paramname="inputName">inputStream</param>

<param name="contentDisposition">attachment;filename=${fileName}</param>

         <paramname="">1024</param>

    </result>

</action>

参数说明 

contentType 

内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片 

 

inputName 

下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream的属性需要编写getInputStream()方法 

 

contentDisposition 

文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为: 

 

attachment;filename="struts2.txt",表示文件下载的时候保存的名字应为struts2.txt。如果直接写filename="struts2.txt",那么默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline;filename="struts2.txt" (我这里是filename=${fileName}需要在action里面定义一个 String filename 然后生成get set方法)

 

bufferSize 

下载缓冲区的大小

 

最后action里面

public String execute(){

         try {

             fileName=URLEncoder.encode("学习下载","UTF-8");

inputStream=new FileInputStream("D:\\apache-tomcat-6.0.41-windows-x64.zip");

         } catch (Exception e){

             e.printStackTrace();

         }

         message="下载成功!";

         return"success";

}

FileName不支持中文。所以这里需要转码!

 

以上就是不刷新页面下载!