日期:2014-05-16  浏览次数:20378 次

jstl自增参数的做法

很久没用jstl的标签了。今天突然用到做在页面做一个由自增ID。复习了一下,写下来给备后用。

html:

?

?

<c:set var="index" value="1"></c:set>
		<c:forEach items="${userlist}" var="u">
			<input type="text" id="${u.name  }${index }"> 
    
    	<c:set var="index" value="${index + 1 }">
			</c:set>

		</c:forEach>

?java:

package com.longflang.action;

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

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;


import com.longflang.vo.User;
import com.opensymphony.xwork2.ActionSupport;

@Results( { @Result(name = "list", location = "/result.jsp") })
public class LoginAction extends ActionSupport {
	private List<User> userlist;
	@Override
	public String execute() throws Exception {
		userlist = new ArrayList<User>();
		userlist.add(new User("longflang", "19"));
		userlist.add(new User("long", "18"));
		userlist.add(new User("flang", "17"));
		return "list";
	}
	public List<User> getUserlist() {
		return userlist;
	}
	public void setUserlist(List<User> userlist) {
		this.userlist = userlist;
	}
	
}

?