JSTL Scriptlet to java class - java

I'm pretty new to Java so bear with me. I have a scriptlet that
is put into a JSTL tag. I'm trying to do away with my scriptlets
and put them in a seperate class. Except I'm really stumped on this
one. I have a pretty basic Java method:
static final String default = "Enter a message";
String subDate(String out){
final String year = "" + Calendar.getInstance().get(Calendar.YEAR);
return out.replaceAll("%CURRYEAR%", year);
}
In my JSTL I call it like below
<c:out value="<%= subDate(msg) %>" default="<%= subDate(default) %>"
When I'm converting this to my Java class this is what I have. But I'm
simply not getting anything. I'm also really confused on how I would
pass it multiple parameters like I'm doing in my JSTL.
public String getsubDate(String in){
return in.replaceAll("%CURRYEAR%", YEAR) + getMsg();
}
Any help is greatly appreciated!

Since it sounds like you're putting msg on the request in the servlet using request.setAttribute, you can get the correct year in the servlet, replace all occurances of %CURRYEAR% right there and just put the correct message on the request:
String year = "" + Calendar.getInstance().get(Calendar.YEAR);
request.setAttribute("msg", msg.replaceAll("%CURRYEAR%", year));
Then on your page just do this:
<c:out value="${msg}" />

Related

Is there way I can show value in textbox in my jsp page even if object is null?

I am having chicken and egg situation:
i have 2 servlets and i want the di object to show through search, not by display servlet
Question:
Is there way I can show the refID in my jsp page even if object is
empty? currently, In my code, i added default query just to fill the "dmsearch" so Jsp don't give
error(SearchDataManagerController.searchDMData(0, 0, 0);).
Or any other solution so my search value should show even display servlet is loaded?
Problem:
On page load, i don't necessarily want to show refId data (servlet 1). if it displays its ok BUT when i click on Search, it should display the data(servlet 2). it displays searched value in textbox but Servlet 1 loads and reset the value back.
Using "Get" in jsp
Code:
<% DataManager di = (DataManager) session.getAttribute("dmsearch"); %>
<input type="text" name="refId" id="refId" value ="<%= di.getiD() %>">
//Servlet display: 1:
DataManager dm;
String buttonClickStatus = request.getParameter("buttonClickStatus");
dm = SearchDataManagerController.searchDMData(0, 0, 0);
session.setAttribute("dmsearch", dm);
//Servlet search: 2:
dm = SearchDataManagerController.searchDMData(driverid, textid, weekid);
session.setAttribute("dmsearch", dm);
please help
Thank you
Solution:
DataManager dm = new DataManager();
int textid=0;
int driverid=0;
int weekid=0;
if(request.getParameter("textid")!=null) {
textid= Integer.parseInt(request.getParameter("textid"));
}
if(request.getParameter("driverid")!=null) {
driverid= Integer.parseInt(request.getParameter("driverid"));
}
if(request.getParameter("weekid")!=null) {
weekid= Integer.parseInt(request.getParameter("weekid"));
}
I'm not sure if this will help you but there is the Expression Language you can read about it, it's so usefull to avoid java code in jsp pages.
You can try this but I'm not sure if this is what you want:
<input type="text" name="refId" id="refId" value ="${ empty di ? 1 : di.getId()}">
Solution:
DataManager dm = new DataManager();
int textid=0;
int driverid=0;
int weekid=0;
if(request.getParameter("textid")!=null) {
textid= Integer.parseInt(request.getParameter("textid"));
}
if(request.getParameter("driverid")!=null) {
driverid= Integer.parseInt(request.getParameter("driverid"));
}
if(request.getParameter("weekid")!=null) {
weekid= Integer.parseInt(request.getParameter("weekid"));
}

jsf commandbutton confirm don't show any message

I have a <h:commandButton> on my page connected with action in my bean. It work just fine, but I wanted to add confirmation message. When I used:
<h:commandButton onclick="confirm('Are you sure?')">
it alco works just fine. But when I try to get string from bean, by making it looks like this:
<h:commandButton onclick="confirm('#{bean.confirmQ}')"> it doesn't display this string. In getter for this string I invoke method to take some info from DB, and I format it then I return it. When I use this approach nothing is shown, not even empty box, and page looks like just refreshing.
Here is code from bean:
private String confirmQ;
public String getConfirmQ() {
WycenioneAuta wa = getWycenioneAuto();
String question = "are you sure \n" + wa.getName + "?";
confirmQ = question;
return confirmQ;
}
public void setConfirmQ(String confirmQ) {
this.confirmQ = confirmQ;
}
Escape the line by writing String question = "are you sure \\n" + wa.getName + "?";
If your String variable is confirmQ , then the right EL pointing to that variable is #{bean.confirmQ} and not #{bean.confirm} as you've written.
In complement to ஜன்'s Answer:
At least for Firefox , I should had a return in the Javascript code, otherwise the cancel does not work:
<h:commandButton onclick="return confirm('Are you sure?')" ... />

Want to pass the java value into javascript function in jsp

I am trying to pass a string value to a JavaScript function by taking from request parameter in JSP, in my struts based project. here is the code:
<%
String timeVal = "Not found";
if(request.getAttribute("myDate")!=null){
timeVal= (String)request.getAttribute("myDate");
}
%>
and then pass it in function as parameter
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('update', <%=timeVal %>)">Save</html:submit>
Where the JavaScript function is
function SubmitPage(action, aa)
{
alert("Date is ...." + aa);
}
But when i try to run this it gives me an error
HTTP Status 400 - Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
With message on web page.
Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
Thanks in advance.
EDIT Here is stack trace
[ERROR] DispatchAction - -Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
it's work for me :
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('<%=timeVal %>')">Save</html:submit>
('<%=timeVal %>') // between single Quotation
Rather using that i will advise you to use value like this in your JavaScript function
var tt = <%=(String)request.getAttribute("myDate")%>
alert(tt+ "Done this....");
Hope this will help you.
Use '<%=timeVal %>' instead of <%=timeVal %> in Javascript method:
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('update', '<%=timeVal %>')">Save</html:submit>

liferay portlet can't get input field value

I can't figure out why i can't get an input value from a jsp. I'm using for cycle to make several input fiels for "choices", but when i'm trying to get values from a mvcportlet, it get nothing.
<aui:form action="<%=addPollURL%>">
<aui:fieldset>
<%
int optionCount = Integer.parseInt(optionCountS);
for (int i = 0; i < optionCount; i++) {
%>
<aui:input label="<%=Integer.toString(i + 1)%>" name="choice<%=i%>"
type="text" />
<%
}
%>
<aui:button-row>
<aui:button value="Add poll" type="submit" />
</aui:button-row>
</aui:fieldset>
</aui:form>
Here goes mvcportlet method
List<String> choices = new ArrayList<String>();
int count = Integer.parseInt(actualChoiceCount);
for (int i = 0; i < count; i++) {
System.err
.println("another choice"
+ ParamUtil
.getString(
actionRequest,
("choice" + i)));
choices.add(new String(ParamUtil.getString(actionRequest,
("choice" + i))));
}
Its really weird... but some ideas or tests
Is AddPollUrl an Action URL with named action and so your action is executed in your generic portlet?
Are you sure text fields are populated with values in the UI (there is no no explicit value in tag)? ParamUtil output would be the same without value that with a blank value
Try without type=text and write it as a single line (input tags)
Try aui:submit instead of aui:button type submit
Try adding an id to form or fields (Ive seen some problems with repeated forms if they dont have namespace)
Why new String(ParamUtil...)?
the most important... whats the output of your System.outs?
That happens because your input field has no value. Or at least it seems so.
You should modify the input to have the value parameter set to "choiceX" like:
<aui:input label="<%=Integer.toString(i + 1)%>" name="choice<%=i%>"
type="text" value="choice<%=i%>" />
Then you'll find it in actionRequest, like Jonny said:
request.getParameter("choice"+i);
This will return you the value of the input field, searching by it's name. So you can have your choice in the processAction method.
Regards !
Try using:
actionRequest.getParameter("choice" + i);
That's not the standard way of getting POST params from the request.

Extend displaytag with struts 1. Problem in keeping current page and sort condition

I am implement displaytag by extend the standard. Please see detail below.
JSP
display:table name="testList" id="obj" requestURI="testAction.do?pageAction=init" pagesize="${paging_size}" sort="list" class="table" **sort="external"** excludedParams="*" decorator="dyndecorator" export="false" **keepStatus="true"**
display:setProperty name="pagination.pagenumber.param" value="page"
display:setProperty name="pagination.sort.param" value="sort" /
display:setProperty name="pagination.sortdirection.param" value="dir"
display:column property="testNo" title="Test no." **sortable="true"** **sortName="testNo"**
display:column property="testValue" title="Test value" **sortable="true"** **sortName="testValue"**
display:table
PaginatedListImpl
public PaginatedListImpl(HttpServletRequest request) {
sortCriterion = request.getParameter("sort");
sortDirection = "desc".equals(request.getParameter("dir"))? SortOrderEnum.DESCENDING : SortOrderEnum.ASCENDING;
pageSize = DEFAULT_PAGE_SIZE;
String page = request.getParameter("page");
index = page == null? 0 : Integer.parseInt(page) - 1;
}
JAVA
public PaginatedListImpl getTest(
Criteria criList = session.createCriteria(TestDto.class);
criList.setFirstResult(pageDisplay.getFirstRecordIndex());
criList.setMaxResults(pageDisplay.getObjectsPerPage());
pageDisplay.setList(criList.list());
pageDisplay.setTotal((Integer) criTotal.uniqueResult());
return pageDisplay;
}
Anyway, I found problem on browser when the browser render the result
when I move the mouse over page_no. It will show "http://localhost:8080/WebProject/testAction.do?page=2&pageAction=init"
when I move the mouse over column name. It will show "http://localhost:8080/WebProject/testAction.do?sort=testValue&*dir=asc*pageAction=init"
but I need the link of both like this
"http://localhost:8080/WebProject/testAction.do?page=2&sort=testValue&dir=asc&pageAction=init"
I search on google many time but I still have a problem.
How can I do? Someone Please help me out of problem.
Thank you a lot..
I am giving you my sample code below
<%
String str = "/details.do?method=showQuestions&surveyId="+arrayNew.get(0);
%>
<html:link page='<%=str%>'><%=arrayNew.get(1)%></html:link>
Try to put url in string variable and use the string variable in the html:link tag as shown above. This may solve your problem.
Thanks

Categories

Resources