系统默认总是 iso-8859-1 的编码, 解决乱码的最佳途径是:
import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import org.apache.struts.action.ActionServlet; public class myActionServlet extends ActionServlet { public myActionServlet() { super(); } protected void process(javax.servlet.http.HttpServletRequest p1, javax.servlet.http.HttpServletResponse p2) throws UnsupportedEncodingException, IOException, ServletException { p1.setCharacterEncoding("Windows-31J"); super.process(p1,p2); } } |
<servlet> <servlet-name>action</servlet-name> <servlet-class>model.common.myActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> |
现有标签对多行文本的输出没有办法,解决的最佳途径是改写 bean:write 标签:
package model.common.tags; import javax.servlet.jsp.JspException; import org.apache.struts.taglib.html.TextTag; import org.apache.struts.util.RequestUtils; import org.apache.struts.util.ResponseUtils; public class WriteTag extends org.apache.struts.taglib.bean.WriteTag { private boolean autobr = false; public int doStartTag() throws JspException { Object bean = null; if (ignore) { if (RequestUtils.lookup(pageContext, name, scope) == null) return (SKIP_BODY); } Object value = RequestUtils.lookup(pageContext, name, property, scope); String output = null; if (value != null) { output = value.toString(); if (filter) output = ResponseUtils.filter(output).replaceAll("\r\n","<BR>"); } if (autobr && (output==null || "".equals(output.trim()) ) ) output = "<BR>"; if(output!=null) ResponseUtils.write(pageContext, output); return (SKIP_BODY); } public void setAutobr(boolean autobr) { this.autobr = autobr; } public boolean isAutobr() { return autobr; } } |
<?xml version = '1.0' encoding = 'Shift_JIS'?> <taglib xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"> <display-name>pjmstag</display-name> <tlib-version>1.0</tlib-version> <short-name>pjmstag</short-name> <uri>/webapp/pjmstag</uri> <tag> <name>write</name> <tag-class>model.common.tags.WriteTag</tag-class> <body-content>empty</body-content> <attribute> <name>autobr</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>bundle</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>filter</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>format</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>formatKey</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>ignore</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>locale</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>name</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>property</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>scope</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> |
<%@ taglib uri="/WEB-INF/pjmstag.tld" prefix="pjmstag"%> <pjmstag:write name="object" property="property" /> |
A 在 public_html/WEB-INF/tags 目录中建立 tag 文件,内容为一些设置及 HTML 片段:
<%@ tag pageEncoding="Shift_JIS"%> <%@ attribute name="PJ_ID" required="true"%> |
C 在 jsp 中使用
<%@ taglib tagdir="/WEB-INF/tags" prefix="upload" %> <upload:PJFileUploadForm PJ_ID="${html.PJ_ID}" /> |
内容 | 描述 |
---|---|
PJ_ID | tag 的属性,传递参数 |
upload | 前缀标识 |
PJFileUploadForm | tag 文件的主名 |
A 配置 actionForm 的文件域
import org.apache.struts.upload.FormFile; public class xxx extends ActionForm { private FormFile file1; public void setFile1(FormFile file1){ this.file1 = file1; } public FormFile getFile1(){ return file1; } } |
B 保存文件流
FormFile f = xxx.getFile1(); InputStream fis = f.getInputStream(); File fl = new File("x:\\folder"); fl.mkdir(); FileOutputStream fos = new FileOutputStream(new File(fl, "filename")); byte[] buffer = new byte[1024]; int len; while( (len=fis.read(buffer))>0 ){ fos.write(buffer,0,len); } fos.close(); |