Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mixing JSP and Scriptlets throws compilation errors

 
Uma Bandaru
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been mixing scriptlets and html in my code as follows:

<% if (%> <jsp:getProperty name="job" property="status"/> <% == "hot"){%>
<option value="hot" selected="true">Hot </option>
<% } else { %>
<option value="hot" selected="true">Hot </option>
<% } %>

Which I assumed is perfectly doable.
But when I try to run the application, it is throwing compilation errors.

Error(20,26): Syntax error, insert ") Statement" to complete IfStatement
Error(20,78): Syntax error on token "==", invalid (
Error(20,78): The left-hand side of an assignment must be a variable
Error(20,86): Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Error(20,86): Syntax error, insert ";" to complete Statement
Error(22,26): Syntax error on token "else", delete this token

Any suggesstions?

Thanks, Uma

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uma Bandaru wrote:Which I assumed is perfectly doable.

If you mean scriptlets and HTML, then sure -- if this were 2001. But it's 2011 and scriptlets have been discredited for almost 10 years now. You should be using the JSTL and EL instead.

But irrespective of the year, it's not scriptlets and HTML you are mixing but scriptlets and JSP actions, which is not doable in any decade.

Any suggesstions?

As stated above. It's time to update your JSP knowledge. JSP 2 was introduced in 2002 -- it's time to catch up.

In JSTL and EL, the following:
becomes
 
Uma Bandaru
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response. I am new to jsp el. Can you please elaborate on this?
After <c:if test="${job.status == 'hot'}"> , can I use "test" as a boolean variable?
Like,

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you check the docs for <c:if> you will see that you can assign it's result to a scoped variable.
 
Uma Bandaru
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, I changed my code to:


But I am not able to view anything inside the <select> tag in the page
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the generated HTML valid?
 
Uma Bandaru
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was the HTML that was generated:

<tr>
<td width="15%">Degree</td>
<td width="25%">

<select name="degree">



<option value = "hot">
</option>






<option value = "medium">
</option>





<option value = "cold">
</option>


</select>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And does that not explain why the options are empty?
 
Uma Bandaru
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, that was silly.. Thanks a lot, it works now.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
Stefan Evans
Bartender
Pie
Posts: 1669
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as an addition to this, rather than writing out such code, I prefer to use a tag which handles it for me like the ones provided by struts/stripes.
ie give it a list of options, and tell it the option that is selected, then let the component render the HTML for it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, I've written many such tags. One for example, is given a Map instance that uses the Map keys as the option value attributes, and the Map values as the option text. Another takes an enum class and constructs a set of option elements from the enum definition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic