欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSTL与struts1标签

程序员文章站 2022-04-17 08:13:29
...

JSTL与struts标签:

http://www.360doc.com/content/06/1013/12/11192_229456.shtml

 

struts标签大全:

http://www.360doc.com/content/09/0819/15/205400_5050517.shtml

 

logic:iterate

http://www.cnblogs.com/nanshouyong326/archive/2008/04/23/1167596.html

 

struts里nested 标签的简单应用

http://www.iteye.com/topic/39959

 

nested使用显示示例:

LoginAction中代码:

LoginForm formM = new LoginForm();
		formM.setUserName("userM");
		formM.setPwd("pwdM");
		Address add = new Address();
		add.setAddrName("slfjslfjlsfjlsf");
		Person person = new Person();
		person.setAdd(add);
		person.setName("goodboy");
		formM.setPerson(person);
		request.setAttribute("formM", formM);

 

jsp中显示的代码:

类的嵌套关系:LoginForm中有Person,Person中Address

public class LoginForm extends ActionForm {
	/*
	 * Generated fields
	 */

	/** pwd property */
	private String pwd;

	/** userName property */
	private String userName;

	private Person person = new Person();
	/*
	 * Generated Methods
	 */

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

 

package com.paul.form;

public class Person {
	private String name;
	private Address add = new Address();
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Address getAdd() {
		return add;
	}
	public void setAdd(Address add) {
		this.add = add;
	}
}

 

package com.paul.form;

public class Address {
	private String addrName;

	public String getAddrName() {
		return addrName;
	}

	public void setAddrName(String addrName) {
		this.addrName = addrName;
	}
}

 

JSP显示:

<br/>
   <nested:root  name="formM">
   		<nested:nest property="person">
   			<nested:write  property="name"/>
   			<nested:nest property="add">
   				<nested:write property="addrName"/>
   			</nested:nest>
   		</nested:nest>
   </nested:root>

 

 

nested标签的应用:

一、nested标签与logic标签类似功能演示:

例如一:

action中:

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.paul.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.paul.form.Address;
import com.paul.form.LoginForm;
import com.paul.form.Person;

public class LoginAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		com.paul.form.LoginForm loginForm = (LoginForm) form;
		request.setAttribute("userName", loginForm.getUserName());
		
		List <String> list = new ArrayList <String> ();
		list.add("1a");
		list.add("");
		list.add("1b");
		request.setAttribute("listData", list);
		
		List <String> list1 = new ArrayList <String> ();
		list1.add("dd");
		list1.add("aa");
		list1.add("bb");
		list1.add("cc");
		request.setAttribute("listData1", list1);
		
		LoginForm form1 = new LoginForm();
		form1.setUserName("user1");
		form1.setPwd("pwd1");
		
		LoginForm form2 = new LoginForm();
		form2.setUserName("user2");
		form2.setPwd("pwd2");
		List <LoginForm> list3 = new ArrayList <LoginForm> ();
		list3.add(form1);
		list3.add(form2);
		request.setAttribute("listData3", list3);
		
		HashMap<String, List> map = new HashMap<String, List>();
		map.put("list", list);
		map.put("list1", list1);
		request.setAttribute("listMap", map);
		
		LoginForm formN = new LoginForm();
		formN.setUserName("userN");
		formN.setPwd("pwdN");
		request.setAttribute("formN", formN);
		
		LoginForm formM = new LoginForm();
		formM.setUserName("userM");
		formM.setPwd("pwdM");
		Address add = new Address();
		add.setAddrName("slfjslfjlsfjlsf");
		Person person = new Person();
		person.setAdd(add);
		person.setName("goodboy");
		formM.setPerson(person);
		request.setAttribute("formM", formM);
		
		
		List maplist = new ArrayList();
		HashMap<String, String> map11 = new HashMap<String, String>();
		map11.put("key1","vlaue1");
		HashMap<String, String> map12 = new HashMap<String, String>();
		map12.put("key2","vlaue2");
		maplist.add(map11);
		maplist.add(map12);
		request.setAttribute("maplist", maplist);
		
		return mapping.findForward("success");
}
}

 JSP中显示:

<%@ page  contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%> 
<%@ taglib uri="/tags/struts-nested" prefix="nested"%> 
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  <head>
    <html:base />
    
    <title>success.jsp</title>

	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  bean:message测试:<br/>
  <bean:message key="message.name"/>
  <br/><br/>
  
  bean:write测试:<br/>
  <bean:write name="userName" scope="request"/>
  <br/><br/>
  
  logic:iterate测试: <br/>
   <logic:iterate id="ld" name="listData" indexId="count">
		<bean:write name="count"/><br/>
		
		<logic:iterate id="dd" name="listData1" indexId="count2">
			<%if(count.intValue()==count2.intValue()){ %>
			<bean:write name="dd"/><br>
			<%} %>
		</logic:iterate>
		<bean:write name="ld"/><br>
   </logic:iterate>
   <br/>
   
  logic:iterate 迭代list<br/>
   <logic:iterate id="kd" name="listData3" indexId="cc">
		<bean:write name="kd" property="pwd"/>
		<bean:write name="kd" property="userName"/><br/>		   	
   </logic:iterate>
   <br/>
   
   nested:iterate 迭代list<br/>
    <nested:iterate id="item" name="listData3" indexId="index">
   		<nested:write name="item" property="pwd"/>
   		<nested:write name="item" property="userName"/><br/>	
   </nested:iterate>
   <br/><br/>
   
   logic:iterate迭代map中的list<br/>
   <logic:iterate id="md" name="listMap">
   		key:<bean:write name="md" property="key"/><br/>
   		value:
   		<logic:iterate id="nd" name="md" property="value">
   			<bean:write name="nd" />
   		</logic:iterate>
   		<br/>
   </logic:iterate>
   
   <br/><br/>
   nested:iterate迭代map中的list<br/>
   <nested:iterate id="md" name="listMap">
   		key:<nested:write name="md" property="key"/><br/>
   		value:
   		<nested:iterate id="nd" name="md" property="value">
   			<nested:write name="nd" />
   		</nested:iterate>
   		<br/>
   </nested:iterate>
   
    <br/> <br/>
   nested:iterate迭代list中的map<br/>
   <nested:iterate id="item" name="maplist" indexId="index">
 	  	<nested:iterate id="itemm" name="item" >
   			<nested:write name="itemm" property="key"/>
   			<nested:write name="itemm" property="value"/>
   		</nested:iterate>
   		
   </nested:iterate>
   <br/>
   
   <br/>
   		<nested:write name="formN" property="userName"/>
   <br/>
   <nested:root  name="formM">
   		<nested:nest property="person">
   			<nested:write  property="name"/>
   			<nested:nest property="add">
   				<nested:write property="addrName"/>
   			</nested:nest>
   		</nested:nest>
   </nested:root>
  </body>
</html:html>

 

 

 

二、nested标签在form提交和与form有关的显示的应用:

 

http://blog.csdn.net/qinglangzhihai/article/details/5791250

 

例如二:

action中:

package com.paul.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.paul.form.NestedForm;

public class NestedAction extends Action {
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		com.paul.form.NestedForm nestedForm = (NestedForm) form;
		List itemList = new ArrayList();
		itemList.add("11111111111");
		itemList.add("11111111122");
		nestedForm.setItemList(itemList);
		return mapping.findForward("success");
}
}

 

form中

package com.paul.form;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts.action.ActionForm;

public class NestedForm  extends ActionForm{
	private String emlId;
	private List itemList = new ArrayList();
	public String getEmlId() {
		return emlId;
	}
	public void setEmlId(String emlId) {
		this.emlId = emlId;
	}
	public List getItemList() {
		return itemList;
	}
	public void setItemList(List itemList) {
		this.itemList = itemList;
	}
}

 

即form中含有list属性,那么在页面中使用nested:iterate显示

<%@ page  contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%>

<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%> 
<%@ taglib uri="/tags/struts-nested" prefix="nested"%> 
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  <head>
    <html:base />
    
    <title>success.jsp</title>

	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <nested:iterate name="nestedForm" property="itemList" id="item">
  		<nested:write name="item"/>
  </nested:iterate>
  </body>
</html:html>

 

 注意这里指定了name为struts配置文件中的form名称

如上使用:

<nested:iterate name="nestedForm" property="itemList" id="item">
  		<nested:write name="item"/>
  </nested:iterate>

 

可输出itemList中的内容

 

 

相关标签: Struts