一尘不染

如何将列表的值传递给隐藏标签?

jsp

我的jsp中有以下代码:

<c:forEach var="value" items="${MyList}" varStatus="loop">
  <tr><td><div id="Name1">Here:${value.name}</div></td></tr>
  <s:hidden id="Name" name="Name" value="%{value.name}"/>

html来源如下:

     <tr><td><div id="Name1">Here:My name is X</div></td></tr>
     <input type="hidden" name="Name" value="" id="Name"/>

如图所示,div标签正确显示了value.name,但hidden标签的value属性为空。我找不到任何解决方案,因为大多数解决方案都与列表无关。我将其更改为,${value.name}但遇到此错误:“根据标签文件中的TLD或属性指令,属性值不接受任何表达式”

我完成的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 prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Register</title>
</head>
<body>
    <div id="Search">
        <label for="message">Travel details</label>
    <table border="1">


    <s:form action="Search_Search" >


      <td>
      <s:textfield name="City" label="City" /></td>
      <td><s:textfield name="HighRate" label="Budget" /></td>
      <td><s:textfield name="Adults" label="Adults"/></td>
      <td><s:textfield name="Childs" label="Child (less 15)"/></td>
      <td><s:select label="Prefered hotel rate" name="rate" list="rate" value="no preference"/></td> 
      <td><s:textfield name="Days" label="Days"/></td>
      <td><s:radio label="Is the budget fixed" name="BdgFixed" list="BdgFixed"
            value="defaultBudget"/>
      <td><s:submit/></td>

      </s:form>


    </table>


    </div>

    <sx:div>
                <table border="1">
                <tr>
                <c:forEach var="Hotel" items="${HotelsList}" varStatus="loop">
                 <c:set var="nameVar" scope="request" value="${Hotel.name}"/>
                 <c:if test="${not loop.first and loop.index % 3 == 0}">
                   </tr><tr>
                 </c:if>
                <td>

                  <div id="HotelInfo">



         <td>
         <s:form action="Search_AddHotel" >
         <tr><td><div id="image"><img src=${Hotel.thumbNailUrl} alt="image"/></div></td></tr>
           <s:textfield name="Name3" label="Budget" />  
         <tr><td><div id="Name1">Here:<c:out value="${nameVar}"/></div></td></tr>
         <s:hidden id="Name" name="Name" value='${nameVar}'/>
         <s:label id="name2" name="name2" value="John"/>
         <tr><td><div id="Highest Rate:">Rate:${Hotel.highRate}</div></td></tr>
         <tr><td><div id="Address">Address:${Hotel.address}</div></td></tr>
         <tr><td>
                      <s:submit/>
        </s:form>

         </td></tr>
          </td>

               </c:forEach>
                    </tr>
                </table>
   </sx:div>

</body>
</html>

我完成的Java类如下:

package com.funontour.controller;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.funontour.common.Hotels;
import com.funontour.model.DistanceModel;
import com.funontour.model.SearchModel;

import java.io.ByteArrayOutputStream;
import java.io.Reader;
import java.io.StringReader;

import javax.xml.soap.*;
import java.net.*;
import java.util.*;

public class SearchAction {

    private String Name;
    private String City;
    private String HighRate;
    private String Adults;
    private String Childs;
    private List HotelsList;
    private List<String> BdgFixed;
    private List<String> rate;
    private static final String Yes = "Yes";
    private static final String No = "No";

    public String execute() throws Exception {
        generateForm();
        // runClient(); //actual rate should be received
        // System.out.println("goes back to strut.xml");
        return "SUCCESS";
    }
    public String getName()
    {
        return Name;
    }
    public void setName(String name){
        this.Name = name;
    }
   public void AddHotel(){
       System.out.println("name is:"+Name);
       System.out.println("getName:" + getName());
       System.out.println("thisName" + this.Name);

   }
    public String Search() { // Country is set to AU
        System.out.println("In Search method of SearchAction");
        Showdetails();
        String result = SearchHotel();

        generateForm();  
        return result;
    }

    public void generateForm(){

        BdgFixed = new ArrayList<String>();
        BdgFixed.add(Yes);
        BdgFixed.add(No);

        rate = new ArrayList<String>();
        rate.add("1");
        rate.add("2");
        rate.add("3");
        rate.add("4");
        rate.add("5");
    }

    public void Showdetails(){
        System.out.println(this.City + " " + this.HighRate + " " + this.Adults + " " 
                + this.Childs + " " + this.rate+ " " + this.BdgFixed);
    }
    public String SearchHotel(){
        HotelsList = new ArrayList();
        SearchModel searchModel = new SearchModel();
        List inputs = new ArrayList<String>();


        //If some of these are not entered should ask again
        // for multiple choices do not work if nothing has been selected
        if(!getCity().isEmpty())
        inputs.add(getCity());
        else
            inputs.add("London");

        if(!getHighRate().isEmpty())
        inputs.add(getHighRate());
        else
            inputs.add("0");

        if(!getAdults().isEmpty())
        inputs.add(getAdults());
        else
            inputs.add("1");

        if(!getChilds().isEmpty())
        inputs.add(getChilds());
        else
            inputs.add("0");

        if(!getRate().isEmpty())
        inputs.add(this.rate.get(0).toString());
        else
            inputs.add("1");

        if(!getBdgFixed().isEmpty()) 
        inputs.add(this.BdgFixed.get(0).toString());   
        else
            inputs.add("Yes");

        if(searchModel.createMessage(inputs) == "SUCCESS")
        {
        System.out.println("SUCCESS");
        setHotelsList(searchModel.getHotelsList());
        return "SUCCESS";
        }
        System.out.println("Failed");
        return "SUCCESS";
    }
    /* Search for the distance      
    public String Search(){


    /*//Search for Suggestion
    public String Search(){
        System.out.println("In Search method");
        SearchModel sm = new SearchModel();
        sm.retrieveItems(getCity().toString());
        return "SUCCESS";
    }*/
    public String getCity() {
        return City;
    }

    public void setCity(String city) {
        City = city;
    }

    public String getHighRate() {
        return HighRate;
    }

    public void setHighRate(String highRate) {
        HighRate = highRate;
    }

    public List getHotelsList() {
        return HotelsList;
    }

    public void setHotelsList(List hotelsList) {
        HotelsList = hotelsList;
    }

    public String getAdults() {
        return Adults;
    }

    public void setAdults(String adults) {
        Adults = adults;
    }

    public String getChilds() {
        return Childs;
    }

    public void setChilds(String childs) {
        Childs = childs;
    }

    public List<String> getBdgFixed() {
        return BdgFixed;
    }

    public void setBdgFixed(List<String> bdgFixed) {
        BdgFixed = bdgFixed;
    }

    public List<String> getRate() {
        return rate;
    }

    public void setRate(List<String> rate) {
        this.rate = rate;
    }

}

%{nameVar}也使用过,但该值仍然为空。:(有人帮助plz


阅读 227

收藏
2020-06-08

共1个答案

一尘不染

尽量不要使用诸如valuekey等之类的属性名称,因为它们可能是保留关键字。

因此,更改value为其他内容,看看是否可行。

编辑:
更改
<s:hidden id="Name" name="Name" value='${nameVar}'/>

<s:hidden id="Name" name="Name" value="%{nameVar}"/>

更新:

如果您想使用forEach标签,请使用
<input type="hidden" name="Name" value="${nameVar}">
<s:iterator>
<s:hidden id="Name" name="Name" value="%{nameVar}"/>

2020-06-08