常用struts标签:
struts-html:
*1 <html:form> <==> <form>
要求:编写该标签,必须创建对应的formBean,同时指定action属性的请求路径
该标签中的action属性,不能直接跳转页面信息
该标签中的action属性,指定跳转资源时,其绝对路径是以项目名开始
2 <html:text>
要求:formBean中必须出现一个跟该控件同名的属性
3 <html:radio>
要求:formBean中必须出现一个跟该控件同名的属性
4 <html:checkbox>
要求:formBean中必须出现一个跟该控件同名的属性
属性类型只能是数组
5 <html:select>
<html:option>
要求:formBean中必须出现一个跟该控件同名的属性
option中的值需要程序员在页面中手动指定
6 <html:select property="salary">
<html:options property="salarys">
要求:formBean中必须出现两个属性,同时在formBean的无参构造方法中对数组、集合进行初始化。
String salary;
String[]/List/Set salarys;
public Form(){
//salarys = new String[]{'1000','2000'....}
salarys = new ArrayList();
salarys.add("1000");
....
}
7 <html:select property="salary">
<html:optionsCollection property="salarys" label="name">
formBean{
private String salary;
private Collection salarys;
public FormBean(){
salarys = new ArrayList();
salarys.add(new Option("10000","10k"));
salarys.add(new Option("20000","30k"));
}
}
public class Option{
private String name; //key属性的名字可以随意指定,只需在页面中的label中一致即可
private String value; //value属性的名字必须是value
set/get
public Option(String name,String value){
this.name = name;
this.value = value;
}
}
struts-bean:
*1 <bean:parameter name="username" id="uname"/>
String uname = req.getParameter("username");
*2 <bean:write name="uname"/>
<%=uname%>
*3 <bean:define id="a" toScope="session" value="aaa"></bean:define>
<==> session.setAttribute("a","aaa");
<bean:write name="a" scope="session"/>
<==> session.getAttribute("a");
4 <bean:define id="aa" toScope="request" scope="session" name="a"></bean:define>
<==>
String a = session.getAttribute("a");
request.setAttribute("aa",a);
5 <bean:write name="a">
<==>pageContext.findAttribute("a");
page-->request-->session-->application
6 <bean:header name="Accept-Language" id="chars"/>
<bean:write name="chars"/>
7 <bean:cookie name="myCookie" value="heheCookie" id="m"/>
<==>
Cookie c = new Cookie("m","heheCookie");
resp.addCookie(c);
<bean:write name="m" property="value"/>
<==>
Cookie[] cs = request.getCookies();
for(Cookie c : cs){
if(c.getName().equals("m")){
<%=c.getValue()%>
}
}
strust-logic:
struts文件上传注意:
1 上传页面中必须使用html:form,同时给该标签指定属性enctype="multipart/form-data",
表示该页面提交给formBean的信息是文件信息,而非普通字符串
2 formBean中必须使用org.apache.struts.upload.FormFile类型接受客户端发送的文件
private FormFile file;
3 action中,获取客户端上传文件的对象流:
formBean.getFile().getInputStream();
action中,获取客户端上传的文件名:
formBean.getFile().getFileName();
将stuMgmt中的showAll.jsp,全部使用struts标签实现