【问题标题】:Using s:url in s:a in struts2在 struts2 中使用 s:a 中的 s:url
【发布时间】:2015-04-27 22:01:14
【问题描述】:

我正在尝试为我的字符串列表中的每个字符串生成一个链接。这个链接将调用一个动作并传入一个参数(一个 getStudentById 动作传入 studentId)。我已经尝试了几种方法来通过在互联网上搜索来使其工作,但是,没有任何运气。最近的尝试导致每个链接都有一个“www.com”的网址,我不知道它来自哪里。下面是几个文件,以帮助我找出问题所在。

WinnerAcknowledgement.jsp

<!DOCTYPE html>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/js/jquery/jquery-ui-1.11.4/jquery-ui.css" type="text/css" media="all"/>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/js/jquery/jquery-ui-1.11.4/jquery-ui.theme.css" type="text/css" media="all"/>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap/bootstrap-datepicker.css" type="text/css" />
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/survey_bh.css" type="text/css" />
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style_bh.css" type="text/css" />
    </head>
    <body>
        <a href='index.jsp'>Home</a>
        <p>Survey saved!</p>
        <p>Congratulations!  You're the winner of the raffle and have just won two movie tickets.</p>
        <div>
            <s:set var="mean" value="db.mean"/>
            <s:set var="standardDeviation" value="db.standardDeviation"/>
            <p><span><strong>Mean:</strong></span>&nbsp;&nbsp;<span><s:number name="%{mean}" type="number" /></span></p>
            <p><span><strong>Standard Deviation:</strong></span>&nbsp;&nbsp;<span><s:number name="%{standardDeviation}" type="number" /></span></p>
        </div>
        <div>
            <p>Students:</p>
            <ul>
                <s:iterator value="studentIds" status="status">
                    <s:url action="getStudentById" includeParams="get" var="url">
                        <s:param name="studentId"><s:property/></s:param>
                    </s:url>
                    <li>
                        <s:a href="%{url}"><s:property/></s:a>
                    </li>
                </s:iterator>
            </ul>   
        </div>
    </body>
</html>

struts.xml

<!--?xml version="1.0" encoding="UTF-8" ?-->
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
  <constant name="struts.devMode" value="false"></constant>
  <constant name="struts.custom.i18n.resources" value="ApplicationResources"></constant>

  <package name="default" extends="struts-default" namespace="/">
    <action name="getStudentById" class="com.action.SurveyAction" method="getStudentById">
      <result name="success">Student.jsp</result>
      <result name="error">NoSuchStudent.jsp</result>
    </action>
    <action name="saveSurvey" class="com.action.SurveyAction" method="saveSurvey">
      <result name="simple">SimpleAcknowledgement.jsp</result>
      <result name="winner">WinnerAcknowledgement.jsp</result>
      <result name="error">error.jsp</result>
    </action>
  </package>
</struts>

【问题讨论】:

  • 显示生成的html。

标签: jsp struts2


【解决方案1】:

代替:

<s:iterator value="studentIds" status="status">
    <s:url action="getStudentById" includeParams="get" var="url">
        <s:param name="studentId"><s:property/></s:param>
    </s:url>
    <li>
        <s:a href="%{url}"><s:property/></s:a>
    </li>
</s:iterator>

我刚试过,效果很好:

<s:iterator value="studentIds" status="status">
    <s:url action="getStudentById" includeParams="get" var="url">
        <s:param name="studentId"><s:property/></s:param>
    </s:url>
    <li>
        <s:a href="%{#url}"><s:property/></s:a>
    </li>
</s:iterator>

%{url} 与 %{#url} 的区别

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 2013-01-08
    • 2023-03-28
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多