struts form action not being called - java

I'm working on an application that uses struts, and I don't fully understand struts yet;
If I have the following in a jsp page:
<form action="/accountProcess" class="" id="" method="post" enctype="multipart/form-data" autocomplete="off">
<select name="user_status_filter">
<option value="no_filter">no filter</option>
<option value="inactive">Inactive</option>
<option value="active">Active</option>
</select>
<input type="submit" value="submit"/>
</form>
and inside my struts-confix.xml:
<action-mappings>
<action path="/accountProcess"
name="UserForm"
type="x.y.UserAction"
scope="request"
parameter="dispatch"
input="pages.account"
validate="false"
roles="user">
</action>
</action-mappings>
and inside my UserAction class:
public ActionForward getUsers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
String requestValue = RequestUtils.getStringParameter(request, "user_status_filter");
return mapping.findForward("adminView");
}
How do I get my form to call the getUsers method in my UserAction class? Do I have to pass some hidden parameter in my form, and if so, how do I do this? In my form, is my action attribute set correctly? And would the form method be post or get?

found the solution, I added a hidden field for the html where the value is the name of the method
<form action="/accountProcess" class="" id="" method="post" enctype="multipart/form-data" autocomplete="off">
<html:hidden property="dispatch" value="getUsers" />
<select name="user_status_filter">
<option value="no_filter">no filter</option>
<option value="inactive">Inactive</option>
<option value="active">Active</option>
</select>
<input type="submit" value="submit"/>
</form>

Related

How to populate dependent combobox in jsp with mysql?

I have registration form jsp page that calls a servlet to add data to mysql database. In the same page I have 2 dropdowns, First one is country which is hard-coded.
According to the selected value, second combobox should populate with the appropriate city_name values before submit the form.
But I can't use php here.
table1-->user(fname,lname,country,city_id)
table2-->city(city_id,city_name,country)
HTML,
<form role="form" name="form1" action="Registration" method="post" class="signup-form">
<div class="form-group">
<select
onchange="document.getElementById('country').value=this.options[this.selectedIndex].text; document.getElementById('idValue').value=this.options[this.selectedIndex].value;">
<option></option>
<option value="sl">Sri Lanka</option>
<option value="jp">Japan</option>
<option value="en">England</option>
<option value="kr">Korea</option>
<option value="us">USA</option>
</select>
<input name="country" placeholder="Select Your Country" id="country" onfocus="this.select()" type="text" required>
</div>
<div class="col-md-4">
<select id="city" name="city" class="form-control">
<option>--Select--</option>
</select>
<label id="city1" class="input-group-error form-error"></label>
</div>
</form>

JSP dynamically change include page

I have a <div> tag:
<div id="content">
<jsp:include page="some.jsp"/>
</div>
Also I have a few buttons:
<input type="submit" name="btn1" value="Page1">
<input type="submit" name="btn2" value="Page2">
I need when I click btn1 some.jsp changes to page1.jsp and to page2.jsp when click btn2.
Use dynamic include,
<jsp:include page="<%= myVariable %>" flush="true" />
Pd: Take a look a flush http://www.coderanch.com/t/484149/JSP/java/flush-true-jsp-include-tag.
1- Instead of using static include, you can use dynamic include, then you can do something like this:
<jsp:include page="${somePage}" flush="true" />
2- Use javascript to change the action of the form depending on the button you click:
<input type="submit" value="Page1" name="btn1"
onclick="document.forms[0].action = 'somePage.jsp'; return true;" />
Im using this solution : My form is :
<FORM>
<select name="choix">
<option value="choix 1">choix 1</option>
<option value="choix 2">choix 2</option>
<option value="choix 3">choix 3</option>
</select>
<input type="submit" />
</FORM>
and im using this in the same jsp page to include a page as what i select in that form:
<%
Ch = request.getParameter("choix");
%>
<div id="inculde_page">
<jsp:include page="<%= "layouts/" + Ch + ".jsp" %>" />
</div>

How to pass a select value to multiple servlets

I am new to JSP, and have a question as below.
In servlet1, I can use request.getParameter() to get the value to SELECT var1.
String ciStr = request.getParameter("var1")
But is there any way I can get the SELECT var1 from servlet2?
Thanks.
JSP code
<form action="Servlet1" method="post" enctype="multipart/form-data">
Confidence Interval
<SELECT name="var1" >
<OPTION value="ci99">99%</OPTION>
<OPTION value="ci95">95%</OPTION>
<OPTION value="ci90">90%</OPTION>
</SELECT> <br>
<textarea name="textArea1" style="width:500px;height:150px;"></textarea> <br>
<input type="submit" value="Submit" size="20" />
</form>
<form action="Servlet2" method="post" enctype="multipart/form-data">
Confidence Interval
<SELECT name="var2" >
<OPTION value="ci99">99%</OPTION>
<OPTION value="ci95">95%</OPTION>
<OPTION value="ci90">90%</OPTION>
</SELECT> <br>
<textarea name=textArea2" style="width:500px;height:150px;"></textarea> <br>
<input type="submit" value="Submit" size="20" />
</form>
Yes, You can use HttpSession object to set the value over server and then get these value any where on server.
HttpSession hs=request.getSession();
and you can set the value with a unique name .
hs.setAttribute("name","value");
and get any where on server by use the name.
String var=(String)hs.getAttribute("name");

Binding form - freemarker + Spring MVC

I have a problem with binding my object in ftl form.
Here is my controller method:
#RequestMapping(method = RequestMethod.POST)
public String saveConfigProperties(#ModelAttribute("configCmdList") ConfigCmdList configCmdList, BindingResult bindingResult) {
configurationDao.setConfigValues(configCmdList.getConfigurations());
return "config";
}
and here is part of my ftl:
<form action="" method="POST">
<#spring.bind "configCmdList" />
<#list configCmdList.configurations as config>
${config.name}
</#list>
<input type="submit" value="submit"/>
</form>
I have an access to my list of objects which I sent previous using GET method in my ftl, but my object list is null after sending object without modifications back to controller.
I tried to bind my configCmdList.configurations and also bind separately each element of that list in loop but without success.
What I'm missing?
VairalPatel web page is down and I remember that he wrote good example about freemarker form and spring mvc.
Thanks in advance for your help.
Ok, I resolved an issue. I had to bind each list element and it parameters separately in loop using ${spring.status.expression} and ${spring.status.value}.
Here is my code:
<form action="" method="POST">
<#list configCmdList.configurations as config>
<#spring.bind path="configCmdList.configurations[${config_index}].id"/>
<input type="hidden" name="${spring.status.expression}" value="${spring.status.value}" />
<#spring.bind path="configCmdList.configurations[${config_index}].name"/>
<input type="text" name="${spring.status.expression}" value="${spring.status.value}" />
<#spring.bind path="configCmdList.configurations[${config_index}].value"/>
<input type="text" name="${spring.status.expression}" value="${spring.status.value}" />
</#list>
<input type="submit" value="submit"/>
</form>
Thank you for your help :)
This is another way to write Caro's solution:
<form action="" method="POST">
<#list configCmdList.configurations as config>
<input type="hidden" name="configurations[${config_index}].id" value="${config.id}"/>
<input type="text" name="configurations[${config_index}].name" value="${config.name}"/>
<input type="text" name="configurations[${config_index}].value" value="${config.value}" />
</#list>
<input type="submit" value="submit"/>
</form>

Unable to fetch value from selectbox to servlet

Trying to submit checked selectbox value(on at a time) along with textbox value to a servlet;here is my code(jsp) :
<script type="text/javascript">
function search(){
document.f2.action="/InfoUser/SearchBox";
document.f2.submit();
}
</script>
<form name="f2">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> ----------- </option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search" onclick="search()">
</div>
</form>
and in the servlet : I have written
String[] select = request.getParameterValues("select");
String search = request.getParameter("search");
Unable to fetch the value in servlet,am I missing some javascript/jquery script to get the value in servlet & how I can use it with the help of JSTL.Any rectification if I'm going wrong .....feel free to comment,will be welcome.
Try to change your form:
<form action="/InfoUser/SearchBox">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> -----------</option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search">
</div>
</form>
Or:
<script type="text/javascript">
function search(){
document.f2.action="/InfoUser/SearchBox";
document.f2.submit();
}
</script>
<form name="f2">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> ----------- </option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search" onclick="search()">
</div>
</form>
You are missing the name of the form and in your javascript you are trying to set the action on non-existing form.
In servlet you have to do:
String select = request.getParameter("select");
String search = request.getParameter("search");
You won't be able to use: String[] select = request.getParameterValues("select"); unless your select element will have multiple="true".

Categories