简体   繁体   English

将out.println转换为c:out

[英]Convert out.println to c:out

What is the JSTL c:out version of this JSP : 什么是此JSPJSTL c:out版本:

<% 

 PersonController personController = new PersonController();
 personController.populate();
 out.println(personController.getAllPeople().get(0).getName()); 

%>

The code running behind this: 在此后面运行的代码:

In PersonController : PersonController

List<Person> allPeople = new ArrayList<Person>();

public void populate() {
  Person person = new Person();
  person.setName("Jeff");
  allPeople.add(person);
}

public List<Person> getAllPeople() {
    return allPeople;
}

public void setAllPeople(List<Person> allPeople) {
    this.allPeople = allPeople;
}

In Person : Person

private String name;

public String getName() {
   return name;
}

public void setName(String name) {
    this.name = name;
}

你需要这个 -

<c:out value="${personController.allPeople[0].name}"/> 

Use the following Expression which works: 使用以下有效的表达式:

<c:out value="${personController.allPeople[0].name }"/>

If the following expression does not work you have other issues within your code. 如果以下表达式不起作用,则您的代码中还有其他问题。 I suspect that an instance of personController is never being bound to the request using setAttribute() or via a JSP tag. 我怀疑personController的实例永远不会使用setAttribute()或通过JSP标记绑定到请求。 At somepoint in the code an instance of personController must be placed within the request or session so that it may be referenced by the JSP EL. 在代码中的某个点,必须将personController的实例放置在请求或会话中,以便JSP EL可以引用它。


Using a Servlet 使用Servlet

I have constructed a GitHub Gist which may provide some insights. 我构建了一个GitHub Gist ,它可能提供一些见解。 Notice in my servlet I create an instance of personController and add it to the request . 注意,在我的servlet中,我创建了personController的实例并将其添加到request I then forward to the JSP containing the expression which resolves the expression and displays the value.. 然后,我转到包含可解析表达式并显示值的表达式的JSP。

PersonController pc = new PersonController();
pc.getAllPeople().add(new Person("Joe"));
pc.getAllPeople().add(new Person("John"));
request.setAttribute("personController", pc);

String nextJSP = "/index.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request,response);

Using a JSP Tag 使用JSP标签

Another method of associating an instance of personController is to use the <jsp:useBean/> tag. 关联personController实例的另一种方法是使用<jsp:useBean/>标记。

UseBean Example JSP UseBean示例JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<jsp:useBean id="personController" class="org.test.PersonController" />

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <c:out value="${personController.allPeople[0].name }"/>
</body>
</html>

This will eliminate the nasty scriplets, but will require you to modify the PersonController class so that the default constructor will invoke the populate() method. 这将消除讨厌的片段,但是将要求您修改PersonController类,以便默认构造函数将调用populate()方法。

org.test.PersonController.java org.test.PersonController.java

public class PersonController {

    List<Person> allPeople = new ArrayList<Person>();

    public PersonController() {
        this.populate();
    }

    public void populate() {
        Person person = new Person("Jeff");
        person.setName("Jeff");
        allPeople.add(person);
    }
    /* Omitted Accessors */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM