我已經將代碼濃縮爲其最基本的要領,以儘可能清楚地說明我的問題。邏輯:目前不按預期工作
我希望格式化結果ok - 這是我的第一篇文章,帶有stackoverflow,無論我做了什麼,縮進4個空格或使用Ctrl-K,編譯器都不開心。
我的addAssignment.jsp在LinkedList應該顯示爲空時顯示標題「Messages」。如果LinkedList中沒有消息,我期望邏輯:present標記排除標題「消息」的打印。如果LinkedList中沒有消息,如何防止打印標題「Messages」?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Add Assignment Page</title>
<style type="text/css">table { margin-left:5%; }</style>
</head>
<body>
<table>
<tr>
<td>
<logic:present name="messages">
<h3>Messages:</h3>
<logic:iterate id="msg" name="messages">
<bean:write name="msg" /><br />
</logic:iterate>
</logic:present>
</td>
</tr>
</table>
<table>
<tr>
<td>
<H1> Add Assignment</H1>
</td>
</tr>
</table>
<html:form action="addAssignmentAction" focus="injuredWorkersFirstName">
<table>
<tr>
<td>First name</td>
<td>
<html:text property="injuredWorkersFirstName" />
</td>
</tr>
<tr>
<td class="100PixCentered"> </td>
<td>
<html:submit>submit</html:submit>
</td>
</tr>
</table>
</html:form>
</body>
</html>
LoginAction文件說明了我如何使用LinkedList。此外,LoginAction可以與Login.jsp頁面,LinkedList以及Logic和Bean標籤一起正常工作。也就是說,當LinkedList中出現錯誤消息時,Login.jsp頁面在消息頭下正確顯示它們。
public class LoginAction extends Action {
public void execute(Object data) { }
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
LinkedList messages = new LinkedList();
String returnValue = "success";
if ((((LoginForm) form).getName() == null)
|| ((((LoginForm) form).getName()).length() == 0)) {
messages.add("bad name");
returnValue = "error";
}
if ((((LoginForm) form).getPassword() == null)
|| ((((LoginForm) form).getPassword()).length() == 0)) {
messages.add("bad password");
returnValue = "error";
}
request.setAttribute("messages", messages);
return mapping.findForward(returnValue);
}
}
Struts配置:
<struts-config>
<data-sources></data-sources>
<!-- ========== Form Bean Definitions =================================== -->
<form-beans>
<form-bean name="loginForm" type="package1.LoginForm" />
<form-bean name="AddAssignmentForm" type="package1.AddAssignmentForm" />
</form-beans>
<!-- ========== Action Mapping Definitions ============================== -->
<!-- Action Mappings Configuration -->
<action-mappings>
<action path="/login" type="package1.LoginAction" name="loginForm"
scope="request" validate="false" input="/index.jsp">
<forward name="success" path="/addAssignment.jsp"></forward>
<forward name="error" path="/index.jsp"></forward>
</action>
<action path="/addAssignmentAction" type="assignment.AddAssignmentAction"
name="AddAssignmentForm" scope="request" validate="false"
input="/addAssignment.jsp">
<forward name="success" path="/mainMenu.jsp"></forward>
<forward name="error" path="/addAssignment.jsp"></forward>
</action>
</action-mappings>
<!-- ========== Message Resources Definitions =========================== -->
</struts-config>