[英]Simple JSP expression language code won't work
I have a Person class and Dog class. 我有一个Person类和Dog类。 Person has-a dog. 人有一只狗。 I send a post http request
to a servlet from a simple html file. 我从一个简单的html文件向servlet发送了一个post http request
。 In the servlet, I add a person with his dog as an attribute to the request. 在servlet中,我添加了一个带有他的狗的人作为请求的属性。 The servlet forwards this request to a jsp which should print the name of the dog using expression language or EL. servlet将这个请求转发到一个jsp,该jsp应该使用表达式语言或EL打印出狗的名字。
I followed all the instructions in my book and I am getting the wrong output. 我按照书中的所有说明进行操作,但输出错误。 Please help me to fix my code. 请帮助我修复代码。
Expected output - Dog's name is: dog. 预期输出-狗的名字是:狗。 Actual output - Dog's name is: 实际输出-狗的名字是:
Servlet's doPost - Servlet的doPost-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Person p = new Person();
Dog d = new Dog();
p.setName("Man");
d.setName("dog");
request.setAttribute("person", p);
RequestDispatcher view = request.getRequestDispatcher("/jsp/MyJsp.jsp");
view.forward(request, response);
}
JSP main code - JSP主要代码-
<html>
<body>
Dog's name is: ${person.dog.name}
</body>
</html>
Full code for Pojos and stacktrace - Pojos和stacktrace的完整代码-
Dog- 狗-
package foo;
public class Dog {
private String name;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
Person - 人-
package foo; 包foo;
public class Person { 公共类人{
String name;
Dog dog;
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public Dog getDog() {return dog;}
public void setDog(Dog dog) {this.dog = dog;}
}
I restarted eclipse and tomcat again and my code worked. 我再次重新启动了eclipse和tomcat,我的代码工作了。
您尚未为该人定下狗。
p.setDog(d);
Your Person
class will need public setter and getter Dog
methods. 您的Person
类将需要公共setter和getter Dog
方法。 Your Dog
class will need public setter and getter Name
methods 您的Dog
类将需要公共setter和getter Name
方法
Eg 例如
public class Person { ...
private Dog dog;
public Dog getDog () {return dog;}
.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.