Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Tag with Attribute

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to runt the program mentioned in HFSJ .pg.521

Where am i supposed to set the attribute for setMovieList.
Is it by using
<jsp:setProperty >
if yes how. Do i have to set two properties - name and genre?


The servlet code is:





JSP Tag Invocation



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you need to set an attribute called "movieCollection" in a scope. There are different ways to do it. One would be to make a servlet, set the "movieCollection" attribute in the request scope, and forward to the JSP file. Can you figure out how to do it ?
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your response , This is what i could come up with ...




But it still doesn't map anyway to the MyTag.jsp
How do I make this work :


And also , how do i link it to SimpleTagAttr.java file...?

If you could give me a little more help , I ll try to figure it out.

Thanks!
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your movieCollection contains Strings, the elements in that list should be Objects of type Movie...
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean creating a Movie class with set and get methods.

And creating Movie objects and setting the name property to the name of the movie..

and then adding them to List...?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Nabila, you were very close

And creating Movie objects and setting the name property to the name of the movie.

Yes, a Movie class with a name and a genre.

Waiting for your next answer.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the file i created (Hope its Correct!) :

Movie.java



MyServlet.java



I still have to create the Web.xml file.
But i cant figure out how the Tag handler class ie.SimlpleTagAttr. java is going to be linked to the Servlet...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still have to create the Web.xml file.

"web.xml", all you need is too declare your servlet and its url mapping. You should have seen this already in your book.

But i cant figure out how the Tag handler class ie.SimlpleTagAttr. java is going to be linked to the Servlet...

Tag handlers are not linked to servlets. Your servlet just puts a List in the request scope, and forwards the request to your JSP file. Once the JSP is called, the container will see your tag, and will look for its class, using a TLD (Tag Library Descriptors). You have to make a TLD for your tag, and put it under WEB-INF. This should also be explained in your book.
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shall have to make the tld file to get the tag working. The sample tld for your code is as mentioned below. You shall have to put this file in web-inf directory.

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the url supposed to be...?

http://localhost:8080/SimpleTagAttr/MyTag.jsp

-OR-


http://localhost:8080/SimpleTagAttr/MiSer.do (And why)


My Web.xml file is


And the rest of the files are same.

I am getting this error with both the urls:


org.apache.jasper.JasperException: Exception in JSP: /MyTag.jsp:8

5:
6: THe movie list is :
7: <table>
8: <myTags:simple movieList="${movieCollection}">
9:
10:
11:
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the url supposed to be...?

You want to call your servlet, which will redirect to the jsp. So you should use the servket mapping that you defined in web.xml : http://localhost:8080/SimpleTagAttr/MiSer.do

I am getting this error with both the urls:

Your tag is not closed.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did close my tag with </myTags:simple> before closing the table.



Either way , its not working.. I am getting the same error
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nabila.

Can you post your JSP file as well.

Kind regards.
Hasnain.


UPDATE: Sorry my bad. its already posted.

Kind Regards.
Hasnain.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the stack trace, what you gave was the output in the browser, check the logs to see what actual exception is coming. Show us the relevant excerpts of the logs. Your getName and getGenre methods are also not public in the Movie class...
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Jsp file is :





I am also getting this along with the previous error message i mentioned.

root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to find a value for "name" in object of class "foo.Movie" using operator "."
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Your getName and getGenre methods are also not public in the Movie class...
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the out put:

He movie list is : Now Naming movie and Genre: Now Naming movie and Genre: Now Naming movie and Genre: Now Naming movie and Genre: Now Naming movie and Genre:
The DaVinci Code Drama
The Wedding Planner Romance
Matrix Action
Live Free or Die Hard Action
Shawshank Redemption Drama


I am not getting any value for ${movieCollection}.
Is it because it's an list of objects...

I guess the error was my get method not being public.

Thanks all for taking the time to give me a response on this!
 
Hasnain Javed Khan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nabila Mohammad wrote:My Jsp file is :





I am also getting this along with the previous error message i mentioned.

root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to find a value for "name" in object of class "foo.Movie" using operator "."



As Ankit said, the getters in the Movie class are not public.make the getters public. That should solve the problem most probably.

Let us know when you make the changes and try it.

kind Regards.
Hasnain.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:It looks like you need to set an attribute called "movieCollection" in a scope. There are different ways to do it. One would be to make a servlet, set the "movieCollection" attribute in the request scope, and forward to the JSP file. Can you figure out how to do it ?
i


What are other ways to do it... Just curious!

And why am I not getting any value for ${movieCollection}.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic