在MyEclipse 下 Web Project 简单EL标签的使用:
在 Struts 框架下:
Test_EL.java
package com.classes;
public class Test_EL {
private String username;
private String password;
private int age;
private String sex;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
TestForm.java
package com.el.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class TestForm extends ActionForm {
private String username;
private String password;
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
TestAction.java
package com.el.struts.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.classes.Test_EL;
import com.el.struts.form.TestForm;
public class TestAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
TestForm testForm = (TestForm) form;// TODO Auto-generated method stub
request.setAttribute("name", "name_zhao");
Test_EL test_el=new Test_EL();
test_el.setUsername("zhao");
test_el.setPassword("password1");
test_el.setAge(1);
test_el.setSex("男");
Test_EL test_e=new Test_EL();
test_e.setUsername("zhao");
test_e.setPassword("password1");
test_e.setAge(2);
test_e.setSex("女");
request.getSession().setAttribute("test_el",test_el);
List list=new ArrayList();
list.add(test_el);
list.add(test_e);
request.setAttribute("list", list);
return mapping.findForward("test11");
}
}
test.jsp
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<!-- jsp:useBean id="test_el" scope="page" class="com.classes.Test_EL"/-->
<html>
<head>
<title>JSP for TestForm form</title>
</head>
<body>
<html:form action="test">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
<table>
<tr>
<td>
${test_el.username}
</td>
</tr>
<tr>
<td>
${test_el.password}
</td>
</tr>
<tr>
<td>
${test_el.age}
</td>
</tr>
<tr>
<td>
${test_el.sex}
</td>
</tr>
<tr>
<td>
<c:if test="${name!=null}" var="t">
wo shi shi wo !
</c:if>
</td>
</tr>
<tr>
<td>
<c:forEach items="${list}" var="tag" varStatus="id">
${id.count} ++${tag.username }++aaaa<br>
${id.index} ++${tag.password }++aaaa<br>
${id.first} ++${tag.age }++aaaa<br>
${id.last} ++${tag.sex }++aaaa<br>
</c:forEach>
</td>
</tr>
<tr>
<td>
<c:out value="name_cou"></c:out>
</td>
</tr>
</table>
</body>
</html>