java

位置:IT落伍者 >> java >> 浏览文章

Struts的标记完成复杂循环


发布日期:2019年11月13日
 
Struts的标记完成复杂循环

使用struts中的<logic:iterate>标记可以完成一些复杂的循环

下面用了一个小例子说明这一点

IterateBeanjava:

package beantest;

import orgapachestrutsactionActionForm;

public class IterateBean extends ActionForm {

private String[] name_coll = null;

public void setName_coll(String[] name_coll) {

thisname_coll = name_coll;

}

public String[] getName_coll() {

return thisname_coll;

}

private String[] name_new = null;

public void setName_new(String[] name_new) {

Systemoutprintln(IterateBean:setName_new(String[]));

thisname_new = name_new;

}

public String[] getName_new() {

return thisname_new;

}

}

IterateActionjava

package beantest;

import javaxservlethttpHttpServletRequest;

import javaxservlethttpHttpServletResponse;

import orgapachestrutsactionAction;

import orgapachestrutsactionActionForm;

import orgapachestrutsactionActionForward;

import orgapachestrutsactionActionMapping;

public class IterateAction extends Action {

public ActionForward execute(

ActionMapping mapping

ActionForm form

HttpServletRequest request

HttpServletResponse response) throws Exception {

IterateBean ibean = (IterateBean) form;

String[] temp = {bitan helen};

ibeansetName_coll(temp);

temp = new String[] {aaaaa bbbbb};

ibeansetName_new(temp);

return mappingfindForward(iterateview);

}

}

iterateviewjsp:

<%@ page contentType=text/html;charset=gb %>

<%@ taglib uri=/WEBINF/strutslogictld prefix=logic %>

<%@ taglib uri=/WEBINF/strutsbeantld prefix=bean %>

<logic:iterate id=iterate name=ibean property=name_coll scope=request offset= >

<logic:iterate id=iterate_new name=ibean property=name_new scope=request offset= >

name: <font color=red><%=iterate%></font> ; code: <font color=red><%=iterate_new%></font><br>

</logic:iterate>

★〓〓〓〓〓〓〓〓★<br>

<bean:include id=lower page=/test/lowerjsp />

<bean:write name=lower filter=false/><br>

</logic:iterate>

lowerjsp:

<font color=blue>hello iterate!</font>

strutsconfigxml:

<?xml version= encoding=ISO?>

<!DOCTYPE strutsconfig PUBLIC

//Apache Software Foundation//DTD Struts Configuration //EN

config__dtd>

<strutsconfig>

<formbeans>

<formbean type=beantestIterateBean name=ibean />

</formbeans>

<actionmappings>

<action path=/iteratetest type=beantestIterateAction name=ibean scope=request >

<forward path=/test/iterateviewjsp name=iterateview />

</action>

</actionmappings>

</strutsconfig>

webxml:

<?xml version= ?>

<!DOCTYPE webapp PUBLIC //Sun Microsystems Inc//DTD Web Application //EN app__dtd>

<webapp>

<servlet>

<servletname>action</servletname>

<servletclass>orgapachestrutsactionActionServlet</servletclass>

<initparam>

<paramname>config</paramname>

<paramvalue>/WEBINF/strutsconfigxml</paramvalue>

</initparam>

<loadonstartup></loadonstartup>

</servlet>

<servletmapping>

<servletname>action</servletname>

<urlpattern>/eye/*</urlpattern>

</servletmapping>

</webapp>

输出效果

name: bitan ; code: aaaaa

name: bitan ; code: bbbbb

★〓〓〓〓〓〓〓〓★

hello iterate!

name: helen ; code: aaaaa

name: helen ; code: bbbbb

★〓〓〓〓〓〓〓〓★

hello iterate!

如上所示一个循环中不能直接将多个变量同时输出但在循环中嵌套循环可以实现一种复杂效果

要实现在一个循环中将多个变量同时输出可以引用<bean:include><bean:write>标记

               

上一篇:Hibernate编写通用数据库操作代码

下一篇:开发框架:深入了解 Struts Validator