/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 13:35:42
* To change this template use File | Settings | File Templates.
*/
public class Clazz {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 13:36:13
* To change this template use File | Settings | File Templates.
*/
public class Student {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
private Clazz clazz;
}
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<action path="/beanWrite" type="com.tag.BeanWriteAction">
<forward name="success" path="/tag_beanwrite.jsp"></forward>
</action>
<action path="/logic" type="com.tag.LogicAction">
<forward name="success" path="/tag_logic.jsp"></forward>
</action>
<action path="/iterator" type="com.tag.IteratorAction">
<forward name="success" path="/tag_iterator.jsp"></forward>
</action>
</action-mappings>
</struts-config>
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import com.model.Clazz;
import com.model.Student;
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 13:30:29
* To change this template use File | Settings | File Templates.
*/
public class BeanWriteAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setAttribute("name","dog");
request.setAttribute("nameHtml","<font color='red'>cat</font>");
request.setAttribute("date",new Date());
request.setAttribute("decimal",1234567.123);
Clazz c=new Clazz();
c.setName("clazzName");
Student s=new Student();
s.setName("张三");
s.setAge("24");
s.setClazz(c);
request.setAttribute("student",s);
return mapping.findForward("success");
}
}
Created by IntelliJ IDEA.
User: wangchenyang
Date: 2010-11-30
Time: 13:40:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<html>
<head><title>BeanWrite示例</title></head>
<body>
<h1>BeanWrite标签测试</h1>
<bean:write name="name"></bean:write><br>
<bean:write name="nameHtml" filter="false"></bean:write><br>
<%--<bean:write name="date"></bean:write><br>--%> <!-- 不能直接解析-->
<bean:write name="date" format="yyyy-MM-dd"></bean:write><br>
<%--<bean:write name="decimal"></bean:write><br>--%> <!-- 不能直接解析-->
<bean:write name="decimal" format="###,###.###"></bean:write><br>
<bean:write name="decimal" format="###,###.0000"></bean:write><br><!-- 0的个数是对应的-->
<bean:write name="student" property="name"></bean:write><br>
<bean:write name="student" property="age"></bean:write><br> <!-- 只能显示字符串 ?!!?实体类中int类型值的设置要考虑这个问题-->
<bean:write name="student" property="clazz.name"></bean:write>
</body>
</html>
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.model.Student;
import com.model.Clazz;
import java.util.List;
import java.util.ArrayList;
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 16:46:17
* To change this template use File | Settings | File Templates.
*/
public class IteratorAction extends Action {
@Override
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
List<Student> list=new ArrayList<Student>();
Clazz c=new Clazz();
//Student student=new Student(); 如果放这的话循环只能得到最后一次循环的值(最后一次把以前的值覆盖了)
c.setName("clazzName");
for(int i=0;i<5;i++){
Student student=new Student();
student.setName("student"+i);
student.setAge("2"+i);
student.setClazz(c);
list.add(student);
}
httpServletRequest.setAttribute("list",list);
return actionMapping.findForward("success");
}
}
Created by IntelliJ IDEA.
User: wangchenyang
Date: 2010-11-30
Time: 16:51:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<html>
<head><title>Iterator标签示例</title></head>
<body>
<h1>Iterator标签示例</h1>
<logic:empty name="list">
没有可以输出的信息
</logic:empty>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>班级</td>
</tr>
<logic:notEmpty name="list">
<logic:iterate id="li" name="list">
<tr>
<td><bean:write name="li" property="name"></bean:write></td> <!--property 后面只跟已经定义的属性值,不用li.name-->
<td><bean:write name="li" property="age"></bean:write></td>
<td><bean:write name="li" property="clazz.name"></bean:write></td>
</tr>
</logic:iterate>
</logic:notEmpty>
</table>
</body>
</html>
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 16:18:49
* To change this template use File | Settings | File Templates.
*/
public class LogicAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setAttribute("a",null);
request.setAttribute("b","");
request.setAttribute("c",new ArrayList());
return mapping.findForward("success");
}
}
Created by IntelliJ IDEA.
User: wangchenyang
Date: 2010-11-30
Time: 16:30:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
<html>
<head><title>Logic示例</title></head>
<body>
<h1>Logic标签测试</h1>
<!--当一个属性值是null时,判断结果为empty和not present-->
<logic:empty name="a">a is empty </logic:empty><br>
<logic:notEmpty name="a">a is not empty</logic:notEmpty><br>
<logic:present name="a">a is present</logic:present><br>
<logic:notPresent name="a">a is not present</logic:notPresent><br>
<!--当一个属性值是“”(空字符串)时,判断结果是empty和present-->
<logic:empty name="b">b is empty</logic:empty><br>
<logic:notEmpty name="b">b is not empty</logic:notEmpty><br>
<logic:present name="b">b is present</logic:present><br>
<logic:notPresent name="b">b is not present</logic:notPresent><br>
<!--当一个属性值是空集合时,判断结果是empty和present-->
<logic:empty name="c">c is empty</logic:empty><br>
<logic:notEmpty name="c">c is not empty</logic:notEmpty><br>
<logic:present name="c">c is present</logic:present><br>
<logic:notPresent name="c">c is not present</logic:notPresent><br>
<!--当在任何范围内都未设置该属性时,判断结果是empty和not present-->
<logic:empty name="d">d is empty</logic:empty><br>
<logic:notEmpty name="d">d is not empty</logic:notEmpty><br>
<logic:present name="d">d is present</logic:present><br>
<logic:notPresent name="d">d is not present</logic:notPresent><br>
<%--当属性为null时,<logic:empty>是为空的<logic:notPresent> 也是不存在的--%>
<%--为""时,是分配了内存地址的,<logic:empty>依然为空--%>
<%--<logic:notPresent>是存在的--%>
<%--属性为实例化的对象时,没有实例化对象赋值empty还是空的[/code][/code]--%>
<%--present却是存在的--%>
<%--这两者的差别可以这么理解--%>
<%--emtpy是判断是否有实际的数据值--%>
<%--而present是看是否给分配过有效的内存地址--%>
</body>
</html>