0

(Oups.. Sorry for my english :) )

In my web application, Struts2 is used as the main Servlet dispatcher and filter. But For some reasons, i have a custom filter and a custom servlet used for a specific url "/book".

But I have some commons jsp... i had some issues when the custom Servlet should display my request attributes in the JSP because of the struts tags (implemented before). So i changed these tag by the jstl taglibs and it works now.

But... In one JSP, the main (lol)... I have a search form.. This JSP is included in several JSPs and could be called by Struts and the custom Servlet..

With only Struts the tag was "< s:form>.." and when the form was submitted, all sended values was kept in the input... But now, because of the custom Servlet i use a simple html form which is calling the struts action "search.do".

As source code is below:

<form method="post" action="<c:out value="${contextPath}"/>/search.do" name="search" id="search">
<input type="text" id="search_searchWord" value="" maxlength="200" size="100" name="searchWord">

<div align="right">
<input type="submit" value="Ok" name="searchButton" id="search">
</div>

<select id="search_searchCrit" name="searchCrit">
<option value="0">Crit1</option>
<option value="1">Crit2</option>
<option value="2">Crit3</option>
</select>

</form>

My problem is the search word and the selected option are refreshed after the submit. I need to keep them !

Is there a way to use the struts taglibs with a Standard Servlet ?

Or Do you have another solution to keep the submitted information ?

Thanks all !

4
  • Can't you simply use two different "common" JSP, one for the Actions, the other for the servlet ? Mar 5, 2014 at 9:39
  • Hi, No because this JSP could be updated with other functionnalities. Thanks but i found a solution.
    – malak
    Mar 10, 2014 at 14:35
  • Consider sharing it by answering your own question to help future visitors with the same problem Mar 10, 2014 at 14:52
  • i was writing it when you have added this comment :p
    – malak
    Mar 10, 2014 at 15:02

2 Answers 2

0

take each field value from the input field and write js function to fill each field in jsp source code of your page.

function selectedValue(){  
    var value =<%=request.getParameter("searchCrit")%>;  
    if(value !=null)  
        {
          document.getElementById('search_searchCrit').innerHTML=value;          

        }    

} 
1
  • Thanks, this help me, i didn't use this solution because javascript is not necessarily required. I just take the <%=request.getAttribute("searchCrit")%>, define a jstl variable and display it as the value in the html input.
    – malak
    Mar 10, 2014 at 14:38
0

I found a solution with the help of @java_seeker.

In my Struts action, i got the request through this way :

HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("searchWord", this.getSearchWord());

There is two different way to do this, see: http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2/

The attribute is setted in each method (in the action) that could refresh the page.

Then, i just recovered and set the attribute from the request as a variable with a jstl tag and display it as the value of my html input:

    <c:set var="searchWord" value='<%=request.getAttribute("searchWord") %>' />
    <input type="text" id="search_searchWor" value='<c:out value="${searchWord}" />' name="searchWord">

For the , i just used an <c:choose><c:when test=""></c:when><c:otherwise><c:otherwise></c:choose> to set the selected choice.

Now all value are always displayed. Maybe it's not the very good way to display share the same JSP between a standard servlet and a Struts action, but it works. I'm open to try a better solution if you have one! Thanks all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.