【问题标题】:struts tag questionsstruts标签问题
【发布时间】:2011-10-03 04:38:33
【问题描述】:

我有两个相关的 struts jsp 文件。 我是新手,有点困惑。

  1. 在 #1 中,对于 td,这些值来自哪里,我的意思是名字、姓氏、部门名称?是来自 java action 还是 hibernate 类的字段?

  2. 在 #2 的第 10 行,什么是员工? employee.employee.id 与#2 代码底部的相同吗?另外,在 s:select 中,departmentID 和 name 是否来自某个类?

非常感谢您的帮助...

1.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html>
<head>
    <link href="<s:url value="/resources/main.css"/>" rel="stylesheet" type="text/css"/>
    <title><s:text name="label.employees"/></title>
</head>
<body>
<div class="titleDiv"><s:text name="application.title"/></div>  
<h1><s:text name="label.employees"/></h1>          
    <table width=600 align=center>
        <tr> <s:url id="insert" action="setUpForInsertOrUpdate"/>
             <td><s:a href="%{insert}">Click Here to Add New Employee</s:a></td>
         </tr>
    </table><br/>
<table align=center class="borderAll">
    <tr>
        <th><s:text name="label.firstName"/></th>
        <th><s:text name="label.lastName"/></th>
        <th><s:text name="label.age"/></th>
        <th><s:text name="label.department"/></th>
        <th> </th>
    </tr>
    <s:iterator value="employees" status="status">
        <tr class="<s:if test="#status.even">even</s:if><s:else>odd</s:else>">
            <td class="nowrap"><s:property value="firstName"/></td>
            <td class="nowrap"><s:property value="lastName"/></td>
            <td class="nowrap"><s:property value="age"/></td>
            <td class="nowrap"><s:property value="department.name"/></td>
            <td class="nowrap"><s:url id="update" action="setUpForInsertOrUpdate">
                       <s:param name="employee.employeeId" value="employeeId"/>
                    </s:url> <s:a href="%{update}">Edit</s:a>

                                <s:url id="delete" action="delete">
                                   <s:param name="employee.employeeId" value="employeeId"/>
                    </s:url> <s:a href="%{delete}">Delete</s:a>
            </td>
        </tr>       
     </s:iterator>
    </table>
</body>
</html>

2.

    <%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <link href="<s:url value="/resources/main.css"/>" rel="stylesheet" type="text/css"/>
</head>
<body>
<center>
<div class="titleDiv"><s:text name="application.title"/></div>
<h1><s:if test="employee==null || employee.employeeId == null">
     <s:text name="label.employee.add"/>
   </s:if>
   <s:else>
     <s:text name="label.employee.edit"/>
   </s:else></h1>

    <table width=600 align=center>
        <tr><td><a href="getAllEmployees.action">Click Here to View Employees</a></td>
         </tr>
    </table>     
     <table>
        <tr><td align="left" style="font:bold;color:red">
              <s:fielderror/>
                  <s:actionerror/>
                  <s:actionmessage/></td></tr>
        </table>

    <s:form>
     <table align="center" class="borderAll">

         <tr><td class="tdLabel"><s:text name="label.firstName"/></td>
                    <td><s:textfield name="employee.firstName" size="30"/></td>
         </tr>
        <tr>
            <td class="tdLabel"><s:text name="label.lastName"/></td>
                            <td><s:textfield name="employee.lastName" size="30"/></td>
        </tr>
        <tr><td class="tdLabel"><s:text name="label.age"/></td>
                            <td><s:textfield name="employee.age" size="20"/></td>
        </tr>
         <tr>
            <td class="tdLabel"><s:text name="label.department"/></td>
            <td><s:select name="employee.department.departmentId" 
                list="#session.departments" 
                listKey="departmentId" 
                listValue="name"
                />
            </td>
             <s:hidden name="employee.employeeId"/>
        </tr>
    </table>

    <table> 
        <tr>
          <td><s:submit action="insertOrUpdate" key="button.label.submit" cssClass="butStnd"/></td>
          <td><s:reset key="button.label.cancel" cssClass="butStnd"/></td>
        <tr>
    </table>                 
   </s:form>
</center>       
</body>
</html>

【问题讨论】:

    标签: jsp tags struts


    【解决方案1】:

    #1 applicationresource.properties 文件中存储了一个键值对。该文件在 WEB-Inf 文件夹中可用,或者它的路径在 WEB.xml 文件中的标签下指定。 <context-param> <param-name></param-name> <param-value>(specify the application resource file path)</param-value> </context-param>

    这个文件包含你的jsp页面中使用的键值对。在那个文件中搜索这些label.firstname,label.lastname,你就会得到整个情况。

    【讨论】:

      【解决方案2】:

      回答您的问题:

      没有。 1. label.firstName 可以从 Struts 维护的任何值堆栈中检索。这可以来自消息资源或来自转发到 jsp 的操作类的属性。更多信息请参考以下文档:

      http://struts.apache.org/2.2.3/docs/tag-syntax.html

      没有。 2.employee.employeeId可以引用转发这个jsp的Action中设置的employee变量。如果您看到转发此 jsp 的 Action 类,您将看到类似 private Employee employee; 以及在对象外部共享此变量的 setEmployee(Employee employee)getEmployee() 方法。如果您看到 Employee 类声明,您将在其中看到employeeId 作为变量。 struts 标签将使用 getter 方法访问此变量。这里是getEmployeeId()。我建议您参考下面的链接并查看可以在 Struts2 中使用的完整标签及其用法:

      http://struts.apache.org/2.2.3/docs/tag-reference.html

      在第二个 jsp 的 s:select 中,发生了对 #session.departments 的迭代。这里departments 可能是迭代发生的部门bean 的列表。此列表也在会话范围内。 name="employee.department.departmentId" 定义要生成的html select 元素的名称。如果在浏览器中加载页面后查看 html 源代码,则可以看到这一点。 &lt;s:select/&gt; 标签生成 html select 以及其中的 html option 元素。在这种情况下,当departments列表上发生迭代时,每个department对象都会被访问,其departmentId被设置为选项元素的value属性,部门对象的name被设置为内容,这您在选择元素中看到。您可以参考上述链接中的 s:select 标记参考以获取更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-27
        • 2014-10-04
        • 2012-01-29
        相关资源
        最近更新 更多