I have a problem with passing parameters from one jsp page to the other one. Code looks like this:
home page:
<jsp:include page="page1.jsp">
<jsp:param name="par1" value="1"/>
<jsp:param name="par2" value="2"/>
<jsp:param name="par3" value="3"/>
<jsp:param name="par4" value="4"/>
</jsp:include>
and reciever:
<c:set var="a" value="${param.par1}" />
<c:set var="b" value="${param.par2}" />
<c:set var="c" value="${param.par3}" />
<c:set var="d" value="${param.par4}" />
<label>
"${a}"<br/>
"${b}"<br/>
"${c}"<br/>
"${d}"<br/>
"${param}"
and the browser displays just empty string.
What is wrong with the param attribute?
EDIT: It is liferay app.
The values get lost when you send the values in the way mentioned because of the scope.
Read about different scopes in servlets and Jsp.
It is better you have a servlet (intermediate b/w Jsp's) before sending the values to another jsp page.
First you send the values to a servlet.In the servlet, you have a HttpServletRequest object.
Create your Param object and set the values.
Set the Param object in HttpServletRequest object using setAttribute().
Then use RequestDispatcher to go to another jsp page.
Now you should be able to see the values.
if your receiver code is inside page1.jsp then it will work.
Related
I want to do something like
<from action="#">
<input type="number" name="id1">
.......
<form>
<c:set var="quantity" value="//value from input field here"/>
Is it possible?
Note: whole code is a single jsp file
Assuming you mean wanting to get the submitted value of the input, you can do something like this:
<c:set var="user" value="${param.id1}" />
I've read several of the answers on this site and have yet to find a solution to inserting tiles attributes into nested jsp files:
My goal is to insert a couple of small strings (defined as tiles attributes in tiles.xml) within a tag in a jsp that is nested.
Example:
I have my baseLayout.jsp which looks like:
<tiles:insertAttribute name="header" />
<tiles:insertAttribute name="securePage" />
<tiles:insertAttribute name="body" />
<tiles:insertAttribute name="sessionTimeout"/>
<tiles:insertAttribute name="footer" />
Within my body.jsp I have the following:
<li>
<s:url id="languageUrl" namespace="/prot" action="rs" >
<s:param name="request_locale">
<s:text name="html.lang.op" />
</s:param>
</s:url>
<s:a href="%{languageUrl}" lang="<s:text name="html.lang.op" />">
<s:text name="html.lang.full.op" />
</s:a>
</li>
Simply put, I want to replace the "/prot" and "rs" values within the url tag to be inserted as tiles attributes (so each individual tiles definition will point to the correct action within this language toggle button).
It should be simple, right? Well, I've been working on this for over 2 hours and it seems as though I can't use tiles attributes at all beyond the first (main) tiles template.
Is there any way I can declare a variable in the main template and simply call it instead of inserting directly from tiles?
Is there another solution?
NOTE: this url tag within body.jsp is in the middle of it, and for certain reasons I can't split up body.jsp into two.
I have the following code:
<bean:define id="hasDocuments" name="BudgetSimulationDetailForm" property="hasDocuments" type="java.lang.Boolean"/>
<%
request.setAttribute("enablebtnRelatedDocs", "true");
request.setAttribute("hasDocuments", String.valueOf(hasDocuments));
%>
I want to remove the scriptlet, I tried using c:set with different scopes but it didn't work.
Is it possible to set a request attribute using JSTL tags?
I tried this and did not work:
<c:set name="enablebtnRelatedDocs" value="true" scope="request"/>
and also
<c:set name="enablebtnRelatedDocs" value="${true}" scope="request"/>
Afterwards there is an include:
<jsp:include page="/gema/jsp/includes/detail/top_Detail.jsp">
<jsp:param name="title_key" value="${title}" />
<jsp:param name="title_bundle" value="buc" />
<jsp:param name="standard_buttons_include" value="true" />
<jsp:param name="typeId" value="53555" />
<jsp:param name="detail" value="budget" />
</jsp:include>
Inside the included JSP the request attribute is not visible, apparently.
Sounds Good, You want to use JSP Standard Tag Library instead of Scriplet.
Yes, It's possible using c:set. Read more about Core Tag Library
<c:set var="enablebtnRelatedDocs" value="${true}" scope="request"/>
<c:out value="${requestScope.enablebtnRelatedDocs }"/>
By default c:set set a attribute in page context. you can set it in any scope.
By default, the JSTL Core library function "set" accepts the following attributes:
JSTL Core set property (credits to tutorialspoint.com) :
value,
target,
property,
var,
scope
You should use "var=" instead of "name=". Hope this helps!
Happy coding!
1:
I am appending the parameters with action but I am getting an exception of on my Struts 2 page.
PWC6212: equal symbol expected
Below is my action with appended parameters code which is to be submitted.
action="MyAction.action?id=<%=request.getParameter("id")%>&name=<%=request.getParameter("name")%>&age=<%=request.getParameter("age")%>&num=<%=request.getParameter("num")%>"
Is the above is the syntax problem? If not then how can we set the parameters as a query string with action?
You should not use Scriptlets (<%= %>)
And, if action is an attribute of a Struts tag (like <s:form>), you can't use scriptlets, you should use OGNL.
Please refer to this question: Struts 2 s:select tag dynamic id for more details
Assumed the action attribute is used with the <form tag. Then the construction
<form name="MyAction.action" action="upload?id=<%=request.getParameter("id")%>&name=<%=request.getParameter("name")%>&age=<%=request.getParameter("age")%>&num=<%=request.getParameter("num")%>" method="POST">
should work with the current context. But in your case given error message (Exception Name: org.apache.jasper.JasperException: equal symbol expected is occurred when <s:form tag is used. So, you cannot use this url in the action attribute. This attribute should contain the plain action name that would be used to find your action.
"How we set parameters as a querystring?"
Actually we do it with <s:param tag. For example, when using hyperlinks
<s:a action="MyAction">
<s:param name="id" value="%{id}"/>
<s:param name="name" value="%{name}"/>
</s:a>
But this construction doesn't work with <s:form tag, unless you applying a special syntax as described in this answer and you definitely want to get these parameters if you do in the action
String quesyString = request.getQueryString();
and this string should not be empty.
However, this usecase is rarely applied, If you don't have a reason to get parameters in such way then as alternative you always can use <s:hidden fields to contain the values of the parameters. For example
<s:form action="MyAction" method="POST">
<s:hidden name="id" value="%{id}"/>
<s:hidden name="name" value="%{name}"/>
</s:form>
These values are passed as a parameters and initialize the action attributes after params interceptor worked. You could also get this parameters directly from the request as
Map<String, String[]> params = (Map<String, String[]>)request.getParameterMap();
However, the more convenient way to do this in your action is to implement ParameterAware.
Just like #AndreaLigios mentioned, you should use Struts2 specified EL, check out the Document here.
If you are using <s:url/>, check out Document please for more information.
Your code should look something like this:
<s:url value="MyAction.action">
<s:param name="id" value="%{#parameters.id}" />
<s:param name="name" value="%{#parameters.name}" />
<s:param name="age" value="%{#parameters.age}" />
<s:param name="num" value="%{#parameters.num}" />
</s:url>
I use MyEclipse to compile my program,and I want to achieve internationalization, so I choose use the fmt tag.
The follow is the code:
<fmt:setLocale value="${param.locale }" scope="session" />
Book says that ${param.locale } can get the browser's default-language. In order to change the language, I use two languages, English and Chinese. Though I set the browser's default-language to English, when I reload the jsp page, the language is always Chinese. Could you tell me what's the matter?
full codes:
<%# taglib prefix="fmt" uri="java.sun.com/jsp/jstl/fmt"; %>
<fmt:setLocale value="${param.locale }" scope="session" /> <fmt:setBundlebasename="loginpage"/> <input type="text" id="text1" /> <br/> <input type="password" id="text2" /> <br /> <input type="submit" id="smb" value="<fmt:message key="login_sub" />" />
No that's not true. The EL param object maps a request parameter name to a single value. If param.locale exists then you can set locale through fmt:setLocale/> tag.
Text from this article - Formatting and internationalization through custom tags
The locale used by the JSTL tags when formatting data is normally
determined by examining the Accept-Language header sent by a user's
browser as part of each HTTP request. If no such header is present,
then JSTL provides a set of JSP configuration variables you can set to
specify a default locale. If these configuration variables have not
been set, then the JVM's default locale is used, which is obtained
from the operating system the JSP container is running on.
and take a look at SO thread - How to set JSTL locale from Java code?