用el 在Struts标签下的jsp页面输出。
request.setAttribute("name",po);
${name}
$(name[0].name)
${person.student.name}
这几种输出逻辑举例
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import po.Student;
import po.dogPO;
public class ELTest extends HttpServlet {
/**
* Constructor of the object.
*/
public ELTest() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
dogPO po = new dogPO ();
Student s=new Student ();
s.name="tom";
po.st=s;
request.setAttribute("name", po);
request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
package po;
public class dogPO {
public Student st;
public Student getSt() {
return st;
}
public void setSt(Student st) {
this.st = st;
}
}
package po;
public class Student {
public String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<%@ page language="java" pageEncoding="ISO-8859-1"
import="po.*,java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>MyJsp.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>
<%--<%
ArrayList list = (ArrayList) request.getAttribute("name");
for (int i = 0; i < list.size(); i++) {
out.println(((dogPO)list.get(0)).getName());
}
%>
<%=((dogPO)((ArrayList) request.getAttribute("name")).get(0)).getName()%>
<logic:iterate id="po" name="name">
<bean:write name="po" property="name" />
</logic:iterate>
--%>
[b] ${name.st.name} [/b]
</body>
</html:html>
request.setAttribute("name",po);
${name}
$(name[0].name)
${person.student.name}
这几种输出逻辑举例
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import po.Student;
import po.dogPO;
public class ELTest extends HttpServlet {
/**
* Constructor of the object.
*/
public ELTest() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
dogPO po = new dogPO ();
Student s=new Student ();
s.name="tom";
po.st=s;
request.setAttribute("name", po);
request.getRequestDispatcher("MyJsp.jsp").forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
package po;
public class dogPO {
public Student st;
public Student getSt() {
return st;
}
public void setSt(Student st) {
this.st = st;
}
}
package po;
public class Student {
public String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<%@ page language="java" pageEncoding="ISO-8859-1"
import="po.*,java.util.*"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>MyJsp.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>
<%--<%
ArrayList list = (ArrayList) request.getAttribute("name");
for (int i = 0; i < list.size(); i++) {
out.println(((dogPO)list.get(0)).getName());
}
%>
<%=((dogPO)((ArrayList) request.getAttribute("name")).get(0)).getName()%>
<logic:iterate id="po" name="name">
<bean:write name="po" property="name" />
</logic:iterate>
--%>
[b] ${name.st.name} [/b]
</body>
</html:html>