Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问JSP页面上JSTL中的嵌套对象_Java_Spring_Jsp_Nested_Jstl - Fatal编程技术网

Java 访问JSP页面上JSTL中的嵌套对象

Java 访问JSP页面上JSTL中的嵌套对象,java,spring,jsp,nested,jstl,Java,Spring,Jsp,Nested,Jstl,我有两门课: 比方说 class ABC{ private String a,b; public void setA(String a){ this.a = a; } public void setB(String b){ this.b = b; } public String getA(){ return this.a; } public String getB(){ return this.b ; } } class XYZ{ private ABC abc; publ

我有两门课:

比方说

class ABC{

private String a,b;

public void setA(String a){
this.a = a;
}

public void setB(String b){
this.b = b;
}

public String getA(){
return this.a;
}

public String getB(){
return this.b ;
}

}



class XYZ{

private ABC abc;

public void setABC(ABC abc){
this.abc = abc;
}

public ABC getABC(){
return this.abc;
}

}
具有类ABC属性的类XYZ

我想在JSTL的帮助下访问其中一个JSP页面中ABC类属性的a或b
以这种方式

${XYZ.ABC.A}

使用JSTL标记是可能的 ${XYZ object name.abc.a}

而不是

public void setABC(ABC abc){
this.abc = abc;
}

public ABC getABC(){
return this.abc;
}
使用

使用此JSTL语法检索信息
${XYZ.abc.a}

要在JSP中引用bean,需要将对象放入请求中

request.setAttribute("xyz", xyz);

然后通过JSP中的
${xyz.abc.a}
访问它

我经常会遇到这个问题,所以我添加了明确的语法,并在最后添加了一个示例

第一个问题是,据我所知,您无法轻松地将多个模型添加到表单中

modelAttribute="productDTO" //is okay

modelAttribute="productDTO, vatDTO" //doesn't work
与其他海报所说的不同,只是尝试而已

${A.B.C}
如果B是一个需要自己的模型的对象,也将不起作用。简单的解决方案是向“productDTO”类添加一个额外的“getter”方法,该方法只返回字符串代码或其他简单类型,而不是创建一个特殊的DTO来容纳所有额外的对象:

   public class ProductDTO implements Serializable {

       private static final long serialVersionUID = -5974170234812308892L;
       VatCode vatCode; //an object

       public String getVatCodeAsString() {
           return vatCode.getVATCode(); //useful if it's a code or something flat
       }

       public VatCode getVatCode() {
           return vatCode(); //the JSP won't know what this model is
       }
然后,您可以在jsp页面中访问此方法:

value="${productDTO.vatCodeAsString}
我用这个,我可以保证它是有效的


但是,如果您需要访问复杂对象,语法的工作原理如下:

在控制器中:

 ProductAdminWebDTO productAdminWebDTO = new ProductAdminWebDTO();
 productAdminWebDTO.setProductDTO(productService.getProductByID(prodNo)); 
 modelAndView.addObject("productAdminWebDTO", productAdminWebDTO);
其中ProductAdminWebDTO包含:

private ProductDTO productDTO;
 private String productName; //(make sure to add correct getter, setters)
其中ProductDTO包含:

private ProductDTO productDTO;
 private String productName; //(make sure to add correct getter, setters)
在JSP中:

 <form:form method="post" id="viewForm"
               action="GET" modelAttribute="productAdminWebDTO">

<form:input path="productDTO" id="productDTO" type="text" value="${productAdminWebDTO.productDTO.productName}"/>


注意,顶级元素是“productAdminWebDTO”,但在路径中,您只添加与包含的变量相对应的元素。但是价值观中的完整路径(正如其他海报所指出的那样)。

这有什么问题?你尝试过什么吗?没有错误,但也没有输出…我尝试过,但没有输出我可以访问xyz属性,但当我尝试访问abc属性,如${xyz.abc.A}没有输出请参考此链接,这家伙也有同样的问题,这可能会在该问题中举例说明expressionok中的分号我将尝试。。。。但我认为这是另外一回事…我的意思是我需要以其他方式访问这件事,而不是像我这样做…请参考此链接,这家伙也有同样的问题,这可能是你的事情请一步一步。。。检查XYZ的对象是否为空。如果不为null,则尝试使用${object of XYZ.abc}。此值是否为空。如果不为空,则检索“a”的值。请参考此链接,这家伙也有相同的问题,这可能会导致问题