之前的準備工作完成,現在算是正式進入struts項目的環節了,首先我們寫一個發表留言的jsp頁面。
右鍵點擊BBS項目
, 選擇 New > JSP,命名新文件為post
. 點擊Finish.post.jsp
將在右邊的編輯器里打開了。
- 首先在編輯器中,將<title></title>中間的文字改成發表留言。
- 在文件頂部,添加以下2個taglib:
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>bean tag為用戶提供了若干tag,主要針對表單中form bean;html tag用來代替普通的html標簽,達到簡化操作的目的。
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
- 在<body>里面添加:
<html:form action="post">
</html:form> -
從IDE的Palette面板的HTML的分類里將Table拉入<html:form action="post">之間,設置rows
8
, columns2。
在<td>之間添加數值
<html:form action="post">
<table border="1">
<tbody>
<tr>
<td>名字</td>
<td><html:text property="name" /></d>
</tr>
<tr>
<td>郵件</td>
<td><html:text property="email" /></td>
</tr>
<tr>
<td>題目</td>
<td><html:text property="subject" /> <html:submit value="發送"/><html:cancel value="重置"/></td>
</tr>
<tr>
<td colspan="2">正文<br>
<html:textarea cols="60" rows="8" property="content" />
</td>
</tr>
<tr>
<td>網站</td>
<td><html:text property="url"/></td>
</tr>
<tr>
<td>圖標</td>
<td>
<html:select property="iconId">
<logic:iterate id="icon" name="NewForm" property="icons">
<option value="<bean:write name='icon' property='id'/>"><bean:write name="icon" property="name"/></option>
</logic:iterate>
</html:select>
</td>
</tr>
<tr>
<td>密碼</td>
<td><html:password property="password"/>(英數8文字內)</td>
</tr>
<tr>
<td>字色</td>
<td>
<html:radio property="font" value="#800000"><font color="#800000">■</font></html:radio>
<html:radio property="font" value="#DF0000"><font color="#DF0000">■</font></html:radio>
<html:radio property="font" value="#008040"><font color="#008040">■</font></html:radio>
<html:radio property="font" value="#0000FF"><font color="#0000FF">■</font></html:radio>
<html:radio property="font" value="#C100C1"><font color="#C100C1">■</font></html:radio>
<html:radio property="font" value="#FF80C0"><font color="#FF80C0">■</font></html:radio>
<html:radio property="font" value="#FF8040"><font color="#FF8040">■</font></html:radio>
<html:radio property="font" value="#000080"><font color="#000080">■</font></html:radio>
</td>
</tr>
</tbody>
</table>
</html:form>
</body>
第一個JSP頁面就完成了。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>留言板</title>
</head>
<body>
<h1><bean:write name="NewForm" property="result" /></h1>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>留言板</title>
</head>
<body>
<h1><bean:write name="NewForm" property="result" /></h1>
</body>
</html>