腊月的季节

SSH2之Struts2学习二

动态方法调用

/struts2/list!方法名.action
控制是否启用动态方法的调用的常量是

1
2
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
//false为禁止使用

使用通配符调用action

1
2
3
<action name="list_*" class="cn.itcast.action" method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>

接受请求参数(get/post)

struts主要采用的就是反射机制
通过action里面的set,get方法进行反射,得到用户发送的数据和发给用户的数据。
复合型数据接受

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Person{
private int id;
private String name;
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public String setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}

表单格式

1
2
3
4
<form action="<%=request.getContextPath()%>/control/employee/list_execute" method="post">
id:<input type="text" name="person.id"/><br/>
name:<input type="text" name="person.name"/><br/>
</form>

类型转换器

局部类型转换器:

  • 必须继承DefaultTypeConverter

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
    public class DateTypeConverter extends DefaultTypeConverter{
    @Override
    public Object convertValue(Map<String,Object> context,Object value,Class toType){
    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMdd"); try{
    if(toType==Date.class){
    String[] params=(String[]) value;
    return dateFormat.parse(param[0]);
    }
    else if(toType==String.class){
    Date date=(Date) value;
    return dateFormat.format(date);
    }
    }
    catch(ParseException e){
    return null;
    }
    }
  • 注册类型转换器
    建立Applet,文件名为ActionClassName-conversion.properties
    内容为
    birthday=cn.item.converter.DateTypeConverter.
    全局类型转换器:
    文件名为xwork-conversion.properties
    内容为
    java.util.Date=cn.item.converter.DateTypeConverter

    访问或添加request/session/application属性

    利用ActionContext达到效果

    1
    2
    3
    4
    5
    6
    7
    public String execute(){
    ActionContext ctx = ActionContext.getContext();
    ctx.getApplication().put("app","应用范围");
    ctx.getSession().put("ses","session范围");
    ctx.put("req","request范围");
    return "message";
    }
1
2
3
$(applicationScope.app)</br>
$(sessionScope.ses)</br>
$(requestScope.req)</br>

迭代标签

1
2
3
4
<%@ taglib uri="http://java.sun.com/jsp/jst1/core" prefix="c"%>
<c:forEach items="$(names)" var="name">
$(name) </br>
</c:forEach>

文件上传

1
2
3
<form enctype="multipartform-data" action="name" method="post">
<input type="file" name="fileload"/>
</form>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private File image;
private String imageFileName;
public File getImage(){
return image;
}
public void setImage(){
this.image=image;
}
public String getImageFileName(){
return imageFileName;
}
public void setImageFileName(){
this.imageFileName=imageFileName;
}
public String execute(){
String reqlpath=ServletActionContext.getServletContext().getRealPath("/images");
if(image!=null){
File savefile=new File(new File(realPath),imageFileName);
if(!saveFile.getParentFile().exists())
{
saveFile.getParentFile().mkdirs();
}
FileUtils.copyFile(image,destFile);
ActionContext.getContext().put("message","上传成功");
}
return "success";
}

设置上传文件大小

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

热评文章