Estou começando a estudar JSP e nas video aulas que estava assistindo o autor usar um exemplo de formulário onde os campos são jogados automaticamente em um bean usando a tag:
no exemplo dele funciona bem (video aula) mas não consigo fazer funcionar no meu teste.
abaixo segue os arquivos de teste:
MyClass.java
package exemplo;
public class MyClass {
private MyPojo myPojo = new MyPojo();
public MyClass() {
System.out.println("Inicializado classe " + getClass());
}
public MyPojo getMyPojo() {
return myPojo;
}
public void setMyPojo(MyPojo myPojo) {
this.myPojo = myPojo;
}
}
MyPojo.java
package exemplo;
public class MyPojo {
private String nome;
private int idade;
public MyPojo() {
nome = "Thales";
idade = 27;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page contentType="text/html" pageEncoding="UTF-8" import="exemplo.*"%>
<jsp:useBean id="myBean" class="exemplo.MyClass" scope="session"/>
<jsp:setProperty name="myBean.myPojo" property="*"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="myPojo" action="add.jsp" method="post">
Enter The Name: <input id="nome" size="30" value=${myBean.myPojo.nome}><br>
Enter The Age: <input id="idade" size="10" value=${myBean.myPojo.idade}><br>
<input type="submit">
</form>
</body>
</html>
add.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean class="exemplo.MyClass" id="myBean" scope="session"/>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
out.print("The name is: " + myBean.getMyPojo().getNome() +
" and The age is: " + myBean.getMyPojo().getIdade());
%>
</body>
</html>
<%@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">
<jsp:useBean id="myBean" class="exemplo.MyClass" scope="session"/>
<jsp:setProperty name="myBean.myPojo" property="*"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="myPojo" action="add.jsp" method="post">
Enter The Name: <input id="nome" size="30" value=${myBean.myPojo.nome}><br>
Enter The Age: <input id="idade" size="10" value=${myBean.myPojo.idade}><br>
<input type="submit">
</form>
</body>
</html>
add.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">
<jsp:useBean class="exemplo.MyClass" id="myBean" scope="session"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
out.print("The name is: " + myBean.getMyPojo().getNome() +
" and The age is: " + myBean.getMyPojo().getIdade());
%>
</body>
</html>
MyClass.java
package exemplo;
public class MyClass {
private MyPojo myPojo = new MyPojo();
public MyPojo getMyPojo() {
return myPojo;
}//fim do get
public void setMyPojo(MyPojo myPojo) {
this.myPojo = myPojo;
}//fim do set
}//fim da classe
MyPojo.java
package exemplo;
public class MyPojo {
private String nome;
private int idade;
public MyPojo() {
nome = "Thales";
idade = 27;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
Deu certo comigo… dá uma olhada no que está diferente
Em último caso…cria um novo projeto do zero faz tudo novamente!
Fiz alguns testes aqui, e notei que eu não consigo alterar o valor de uma propriedade dentro do objeto myPojo que esta em myBean. Da erro falando q a propriedade não existe e eu tentei fazer com o <jsp:setProperty nome=“myBean.myPojo” property=“nome” value=“Teste”/> dentro do add.jsp.
eu nao to entendendo mais nada rs…
alguém poderia me explicar o funcionamento correto dos beans dentro do jsp?
org.apache.jasper.JasperException: Cannot find any information on property 'myPojo.*' in a bean of type 'exemplo.MyClass'
org.apache.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:365)
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:308)
org.apache.jsp.index_jsp._jspService(index_jsp.java:65)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)