Struts的中文问题,由3部分组成
1. jsp提交数据的时候,必须是UTF-8编码的
2. struts拿到数据后进行UTF-8解码
3. 服务端跳转到jsp进行显示的时候,要指定浏览器使用UTF-8进行显示
UTF-8可以换成GBK或者GB2312,但是必须统一,不能混用
提交数据的 addProduct.jsp
提交数据的时候指定编码方式UTF-8
并且设置form 的method为
post
方式
struts.xml
指定解码方式为UTF-8
<
struts
>
<
constant
name
=
"struts.i18n.encoding"
value
=
"UTF-8"
></
constant
>
</
struts
>
显示数据的 show.jsp
服务端跳转到jsp进行显示的时候,要指定浏览器使用UTF-8进行显示
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
在tomcat启动struts web应用的时候,如果出现了struts配置上的错误,你可能只能看到一个 Error FilterStart的提示,而看不到详细的错误原因。
这样就加大了定位和解决问题的难度
这是因为默认配置下,struts把日志输出关闭了
为了把日志输出开启便于调试,需要增加log4j.xml这个配置文件
在src目录下增加log4j.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<
appender
name
=
"STDOUT"
class
=
"org.apache.log4j.ConsoleAppender"
>
<
layout
class
=
"org.apache.log4j.PatternLayout"
>
<
param
name
=
"ConversionPattern"
value
=
"%d %-5p %c.%M:%L - %m%n"
/>
</
layout
>
</
appender
>
<!-- specify the logging level for loggers from other libraries -->
<
logger
name
=
"com.opensymphony"
>
<
level
value
=
"DEBUG"
/>
</
logger
>
<
logger
name
=
"org.apache.struts2"
>
<
level
value
=
"DEBUG"
/>
</
logger
>
<!-- for all other loggers log only debug and above log messages -->
<
root
>
<
priority
value
=
"INFO"
/>
<
appender-ref
ref
=
"STDOUT"
/>
</
root
>
</
log4j:configuration
>
关闭log4j.xml日志输出
关闭log4j.xml日志输出很简单,直接把log4j.xml命名成其他文件即可,以后要用再把名字改回来,很方便
在struts中也可以获取servlet包中的request和response对象
public
String show() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
System.out.println(
"request:\t"
+ request);
System.out.println(
"response:\t"
+ response);
product =
new
Product();
product.setName(
"iphone7"
);
return
"show"
;
}
struts中的Session有两个
一个是传统的servlet包下的HttpSession
另一个是Struts中自己定义的Session
传统的
servlet包下的session的获取办法是:
ServletActionContext.getRequest().getSession();
使用该方法,需要在eclipse的项目中导入servlet-api.jar,可以在右边下载
新的Session
的获取办法是
Map m = ActionContext.getContext().getSession();
这个session以Map类的形式出现,其中的值和HttpSession中的值是同步的
在ProductAction的add方法中访问Session
在add方法中获取Struts定义的Session
Map m = ActionContext.getContext().getSession();
然后把 name放进去
public
String add() {
Map m = ActionContext.getContext().getSession();
m.put(
"name"
, product.getName());
return
"show"
;
}
在show.jsp中把通过EL表达式把session中的数据取出来
EL表达式可以直接从session中把数据取出来
<%@page isELIgnored="false"%>
${product.name}
${name}
与通过Servet进行上传文件比较起来,Struts的上传文件就简单很多
首先准备 upload.jsp
上传一定要为form加上enctype="multipart/form-data",表示提交的数据是二进制的
并且必须是
method="post"
这里name使用doc会和服务端的相应代码匹配
<
form
action
=
"upload"
method
=
"post"
enctype
=
"multipart/form-data"
>
上传文件 : <
input
type
=
"file"
name
=
"doc"
/> <
br
>
<
input
type
=
"submit"
value
=
"上传"
>
</
form
>
在upload.jsp中file字段对应的name是"doc"
所以在action中,必须准备3个属性,分别是
File
doc
;
String
doc
FileName;
String
doc
ContentType;
属性名字不能使用其他的,必须基于“
doc
"
然后为这3个属性提供getter setter
package
com.how2java.action;
import
java.io.File;
import
java.io.IOException;
import
java.util.UUID;
import
org.apache.struts2.ServletActionContext;
public
class
UploadAction {
File doc;
String docFileName;
String docContentType;
public
File getDoc() {
return
doc;
}
public
void
setDoc(File doc) {
this
.doc = doc;
}
public
String getDocFileName() {
return
docFileName;
}
public
void
setDocFileName(String docFileName) {
this
.docFileName = docFileName;
}
public
String getDocContentType() {
return
docContentType;
}
public
void
setDocContentType(String docContentType) {
this
.docContentType = docContentType;
}
public
String upload() {
System.out.println(doc);
System.out.println(docFileName);
System.out.println(docContentType);
return
"success"
;
}
}
为upload路径配置UploadAction,并返回success.jsp
<
action
name
=
"upload"
class
=
"com.how2java.action.UploadAction"
method
=
"upload"
>
<
result
name
=
"success"
>success.jsp</
result
>
</
action
>
success.jsp
分别显示doc,docFileName,docContentType
<%@page isELIgnored="false"%>
uploaded success
${doc}
<
br
/>
${docFileName}
<
br
/>
${docContentType}
<
br
/>
页面上是只提交了doc这么一个域。 Struts会判断当有这么一个type="file"的 doc提交的话,就会去Action里看看有没有setDocContentType() ,setDocFileName() 如果有,就注入相关信息。 这是Struts的默认行为
struts上传文件的大小默认是比较小的只有2M,可以进行设置
struts.xml
设置上传文件最大大小为10m
<constant name="struts.multipart.maxSize" value="10240000"/>