JSTL核心库常用标签举例(详解)
struts-config.xml:
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
>
演示的JSP页面(jstlcore.jsp):
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Username | Age | Group |
there is no info! | ||
${user.username } | ${user.age } | ${user.group.name } |
所需的JSTLCoreAction页面(JSTLCoreAction.java):
package com.yulin.struts;
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;
public class JSTLCoreAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("core", "core");
request.setAttribute("core1", "");
//输出html文件
request.setAttribute("core2", "输出html文件");
request.setAttribute("core3", "");
request.setAttribute("v1", 1);
request.setAttribute("v2", 2);
request.setAttribute("v3", "");
List userList = new ArrayList();
for(int i=0;i<50;i++){
Group g = new Group();
g.setName("China");
User u = new User();
u.setUsername("LinZhang" + i);
u.setAge(23);
u.setGroup(g);
userList.add(u);
}
request.setAttribute("userList", userList);
request.setAttribute("fortokens", "a,b,c,d,e,f,g");
return mapping.findForward("success");
}
}
涉及到的User(User.java)和Group(Group.java)页面:
User.java:
package com.yulin.struts;
public class User {
private String username;
private int age;
private Group group;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
Group.java:
package com.yulin.struts;
public class Group {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}