Reputation: 2127
How would I assign a variable within scriplet code in JSP <%> and then use struts logic tags to do stuff based on the value of the variable assigned in the scriplet code block?
I have tried using struts:logic equal and greaterthan to no avail....
Many Thanks,
Upvotes: 1
Views: 1051
Reputation: 72
I guess u find this:
scriptlet code u have to write Java code on JSP
<%int var=1; %>in jsp its declaration ( <%! int i = 0; %> )
The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression
<p> Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
thanks
Upvotes: 1
Reputation: 213
In scriptlet:
<%
request.setAttribute("customerName", "rajesh");
%>
And you can check in struts logic tags like,
<logic:match name="customerName" value="Vijay"></logic:match>
Upvotes: 1
Reputation: 557
You can set a variable in Struts2 using tags. for Example:
<c:set var="contains" value="true" />
logic can be tested:
<c:if test="%{#variable=='String 1'}">
This is String 1
</c:if>
other sources: http://www.mkyong.com/struts2/struts-2-if-elseif-else-tag-example/
Required taglib:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Upvotes: 0
Reputation: 1564
What you are trying to do (if I understand you correct) is basically this:
<% String foo = "Test"; %>
<bean:write name="foo" />
Which, as you already know, doesn't work. That would give an error like this:
Cannot find bean foo in any scope
What I usually do, is to put my data in the page scope like this:
<% pageContext.setAttribute("foo", "Test"); %>
<bean:write name="foo" />
(This is for Struts 1.1. Newer versions may provide a better way to do it.)
Upvotes: 1