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>