分类: Java
2008-04-15 12:49:20
一.html标签
1.html元素的标签:
a1.
a2.
a3.
如:包含请求参数:
包含单个请求变量:<% String stringBean = "Value to Pass on URL";
pageContext.setAttribute("stringBean", stringBean);%>
包含多个请求变量: <% java.util.HashMap myMap = new java.util.HashMap();
myMap.put("myString", new String("myStringValue") );
myMap.put("myArray", new String[] { "str1", "str2", "str3" });
pageContext.setAttribute("map", myMap);%>
b.
2.基本表单标签:
FormBasicForm fbf = (FormBasicForm) form;
if (isCancelled(request)) { fbf.setStatus("Cancel was pressed!");
return (mapping.findForward("success")); }//表示取消选中
else { fbf.setStatus("Submit was pressed!");
return (mapping.findForward("success")); }
3.下拉列表和多选列表
<% Vector colorCollection = new Vector(); colorCollection.add(
new org.apache.struts.util.LabelValueBean("Pink", "htmlselect.pink"));
colorCollection.add( // Pink为label htmlselect.pink为value
new org.apache.struts.util.LabelValueBean("Brown", "htmlselect.brown"));
pageContext.setAttribute("colorCollection", colorCollection); %>
labelProperty="label" /> // labelProperty显示值
4.显示错误消息: errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("key") );
a.显示全局:
b.显示特定字段: errors.add("checkbox1", new ActionMessage("error.checkbox"));
5.显示信息
a.
则:ActionMessages actionMessages=new ActionMessages();
actionMessages.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage(“key”));
saveMessages(request,actionMessages);
b.从一定的范围中查找:
则:protected void saveMessages(HttpServletRequest request,ActionMessages messages){
...request.setAttribute(GLOBAL_MESSAGE_key,messages); }
二:Struts Bean标签
1.访问http请求信息和JSP对象
a.
<% if (cookie.getValue().equals("firsttime")) {
Cookie c = new Cookie("cookiedemo", "Hi Linda!");
c.setComment("A test cookie");c.setMaxAge(3600);response.addCookie(c);} %>
输出:
b.
c.
d.
检索多值:
通过链接传递参数:
循环输出:<% for (int i=0; i
2.访问WEB应用资源:
a.
<% request.setAttribute("stringBean","hello"); SomeBean bean=new SomeBean();
bean.setName("hello"); request.setAttribute("someBean",bean); %>
通过变量名或javaBean得到key值再访问资源文件:
b.
3.定义或输出javaBean,bean:write标签filter为true时会将特殊符号转换成普通字符串
a.value属性:
name和property属性:<% request.setAttribute("sessionBean", session); %>
// contextBean为javax.servlet.ServletContext类型,实例化
name和type属性(用于复制):
type="javax.servlet.ServletContext"/>
三.Struts Logic标签:
1.逻辑判断:(greatEqual,lessEqual,greatThan...)
<% Cookie c = new Cookie("username", "Linda"); c.setComment("A test cookie");
c.setMaxAge(3600); response.addCookie(c); %>
a.
b. <% SomeBean bean=new SomeBean(); bean.setName("Linda");
request.setAttribute("someBean",bean);%>
c. <% request.setAttribute("number","100"); %>
2.字符串匹配:变量中是否包含指定的字符串
<% request.setAttribute("authorName", "LindaSun");%>
a.
3.判断指定内容是否存在
<% ActionErrors errors = new ActionErrors();
errors.add("totallylost", new ActionMessage("application.totally.lost"));
request.setAttribute(Globals.ERROR_KEY, errors);
request.setAttribute("myerrors",errors);request.setAttribute("emptyString","");%>
a.
b.
判断指定的安全角色,用户,cookie,header或javaBean是否存在
c.
从Globals.MESSAGE_KEY中检索,不同于Globals.ERROR_KEY
从指定的ActionMessages对象中检索
4.请求转发或重定向a.
b.
5.循环遍历//offset为开始位置,indexId为序号
a. 遍历集合<% Vector animals=new Vector(); animals.addElement("Dog");
animals.addElement("Cat"); animals.addElement("Bird");
request.setAttribute("Animals", animals);%>
b. 遍历Map<% HashMap h= new HashMap();String fruits[ ] = {"apple","orange","banana"};
h.put("Fruits", fruits); request.setAttribute("catalog", h); %>
//如果value不是集合就不用嵌套了
四.利用Tiles模板和Tiles组件创建复合式网页
1.在web.xml中配置所需要的
2.建立tiles的xml文件放在WEB-INF下:
"-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
"">
<put name="header" value="header.jsp"/>
3.在struts配置文件中配置TilesPlugin插件(ctrl+n):
4.在Action已经存在ActionServlet,并在struts配置文件中配置Action来调用Tiles组件
parameter="index-definition">
ForwardAction为Action内置对象,专门负责转发功能,在将请求转发给parameter的组件
5.在index.jsp中插入Tiles组件<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
6.在layout.jsp文件中进行布局并设计好其他页面:
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
备注:在需要出现的页面只有写
7.Tiles组件的组合(type="definition")以示区别
//将一个组件put成另一个组件的元素