struts标签是为了减少代码量,而是用的一种方便快捷的输出方式;这次是struts1.x版本;
1:首先搭建好的struts1.x的环境;
2:在示例程序中的struts-config.xml找到<message-resources parameter="MessageResources" />放到struts-config.xml中;
3:在java目录下找到MessageResources.properties并拷贝到src目录下;
下面看代码理解:
BeanwriteTest.java:
package com.keith; import java.util.Date; 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 BeanWriteTest extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //普通属性 request.setAttribute("hello", "hello struts_taglibs"); //html文本 request.setAttribute("man","<font color='red'>keith</font>"); //日期 request.setAttribute("date",new Date()); //数字 request.setAttribute("n",124578.454); //结构 Group group = new Group(); group.setGroupName("keithClass"); User user = new User(); user.setName("keith"); user.setAge(20); user.setGroup(group); request.setAttribute("user",user); return mapping.findForward("beanwriteSuc"); } }
由于用到了的结构:
user.java:
package com.keith; public class User { private String name; private int age; private Group group; public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Group.java:
package com.keith; public class Group { private String groupName; public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } }
struts-config.xml中:
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <action-mappings> <action path="/beanWritetest" type="com.keith.BeanWriteTest" > <forward name="beanwriteSuc" path="/beanwrite.jsp"></forward> </action> </action-mappings> <message-resources parameter="MessageResources" /> </struts-config>
beanwrite.jsp:首先要在jsp页面中使用struts标签的话,需要加入:
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
这句话是设定struts的位置及版本,在struts.jar/META-INF/tlds的目录下可以找到;
下面是<body></body>中的代码:
<h2>测试beanwrite</h2> <hr color="orange" size="4"><br> hello(jsp脚本):<%= request.getAttribute("hello") %><br> hello(标签):<bean:write name="hello"/><p></p> html文本:<br> man(default):<bean:write name="man"/><!-- 原样输出了 --><br> man(filter=false):<bean:write name="man" filter="flase"/><p></p> 日期(default):<bean:write name="date"/><br> 日期(format="yyyy-MM-dd HH:mm:ss"):<bean:write name="date" format="yyyy-MM-dd HH:mm:ss"/><p></p> 数字(default):<bean:write name="n"/><br> 数字(format="###,###.#"):<bean:write name="n" format="###,###.#"/><br> 数字(format="###,###.#"):<bean:write name="n" format="###,###.00000"/><p></p> 结构:<br> 姓名:<input type="text" value="<bean:write name="user" property="name"/>"><!-- property="name" 相当于调用了user.getName()方法,标签是在服务器端执行 --> <br> 年龄:<input type="text" value="<bean:write name="user" property="age"/>"> <br> 所属组:<input type="text" value="<bean:write name="user" property="group.groupName"/>"> <br>
在index.jsp中给个请求:
<a href="beanWritetest.do">测试beanWritetest</a>