Acceptable extensions in file dialog - java

How can I set acceptable extensions in file dialog using JSP and Spring MVC? This is how I do:
<form:form method="post" enctype="multipart/form-data"
modelAttribute="uploadedFile" action="fileUpload">
<table>
<tr>
<td>Upload File:</td>
<td><input type="file" name="file" /></td>
<td style="color: red; font-style: italic;"><form:errors
path="file" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Upload" /></td>
<td></td>
</tr>
</table>
</form:form>
Can I do something like this with Spring:
<input type="file" name="file" ext="xml, txt"/>

Try this..
<html>
<body>
<input type="file" name="fi" accept="text/xml, text/plain"/>
</body>
</html>
You don't need spring to do this. It is just simple HTML.

Related

JasperException when using f: in jsp page

I'm trying t learn Spring MVC and my project is buid a jsp form input a product
I added all jar jstl lib, and here is my taglib
<%#taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<%#taglib uri="http://www.springframework.org/tags" prefix="s"%>
Here is my form appears bug
`<f:form action ="insertProduct.htm" method="GET" commandName="newProduct">
<table border ="1" cellpadding="2" cellspacing="2">
<tr>
<td>Product ID</td>
<td><f:input path="productId"/></td>
</tr>
<tr>
<td>Product Name</td>
<td><f:input path="productName"/></td>
</tr>
<tr>
<td>Price</td>
<td><f:input path="price"/></td>
</tr>
<tr>
<td>Description</td>
<td><f:input path="description"/><td>
</tr>
<tr>
<td>Status</td>
<td>
<f:select path="status">
<f:option value="true" label="Active"/>
<f:option value="false" label="Inactive"/>
</f:select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Insert"/>
<input type="reset" value="Reset"/>
<input type="button" value="Back" onclick="history.go(-1)"/>
</td>
</tr>
</table>
</f:form>`
But i cant fix my bug. Please help me :(

SpringMVC - Send nested form in jsp

I have a form (addPassForm) which is nested in another form (addStoreFrom) in the JSP page. How can I send the addPassForm form to the controller?
When I try to send the addPassForm form, addStoreFrom form sends instead.
<s:url value="/addStore" var="urlAddStore"/>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<table border="1">
<tbody>
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</tbody>
</table>
</form:form>
It is just because nested forms in not a valid HTML pattern. The browser simply ignore the inner <form></form> tags and sees only one form. Reference : Is it valid to have a html form inside another html form?
It is not a JSP problem (nor a Java one !), but only a incorrect HTML problem. You must use successive forms instead of nested forms (or user javascript as other suggested)
Example with successive forms :
<s:url value="/addStore" var="urlAddStore"/>
<table border="1">
<tbody>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</form:form>
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
</tbody>
</table>
Explicitly call submit() function in javascript.Add a function and bind it to onclick of add button.<input type="button" value="Add" onclick="submitAddPassForm()"/>.In javasrcipt simply use:
function submitAddPassForm(){
$('#addPassForm').submit();
}
You can just make a ajax post call instead of a form submit

How to send values to a servlet from a jsp which has directly retrieved values from another jsp

my motive is data flow from modify.jsp to modifysummary.jsp then to servlet to update details but i dont want to use scriptlets in my jsp page.
modify.jsp
<form action="modifysummary.jsp">
<table border="1">
<tr>
<td><label>FirstName</label></td>
<td><input type="text" name="firstname" value="<%=bean.getFirstname() %>"></td>
</tr>
<tr>
<td><label>Surname</label></td>
<td><input type="text" name="surname" value="<%=bean.getSurname() %>" ></td>
</tr>
</form>
modifysummary.jsp
<table>
<tr >
<th>Employee Details</th>
</tr>
<tr>
<td>First Name</td>
<td><%=request.getParameter("firstname") %></td>
</tr>
<tr>
<td>Surname</td>
<td><%=request.getParameter("surname") %></td>
</tr>
<tr>
<td><input type="button" value="BACK" onclick="javascript:history.go(-1)"></td>
<td><input type="submit" value="Modify" name="Modify"></td>
</tr>
</table>
i have written this code inside a form,on clicking the modify button the data should go to servlet in a bean but how to do that.
i think this is not possible without using scriptlets in your modifysummary.jsp . you must set your username and password into request attributes
using request.setattribute();
and get them in servlet
using request.getattribute();
why you don't want to use scriptlets.
and if you want to use requestdispatcher you have to write all that in scriptlets

Jump between dynamic textfield

I want to jump between textfields, but these fields are generated dynamically through an iterator from struts-tags:
<s:iterator value="aList">
<td width="50px" align="center">
<s:textfield name="solField" size="2" maxlength="1" style="text-transform: uppercase; text-align:center"/>
</td>
</s:iterator>
I tried jumping with javascript but having the same field name is not working properly.
The code in the browser is (with three items in the list to iterate):
<td width="50px" align="center">
<input type="text" name="solField" size="2" maxlength="1" value="" id="correct_solField" style="text-transform: uppercase; text-align:center"/>
</td>
<td width="50px" align="center">
<input type="text" name="solField" size="2" maxlength="1" value="" id="correct_solField" style="text-transform: uppercase; text-align:center"/>
</td>
<td width="50px" align="center">
<input type="text" name="solField" size="2" maxlength="1" value="" id="correct_solField" style="text-transform: uppercase; text-align:center"/>
</td>
Any ideas?
Thanks in advance
How about this? (using jQuery)
$(document).on('keyup', 'input', function(){
if($(this).val().length >= $(this).attr('maxlength'))
{
$(this).nextAll('input:first').focus();
// OR even
// $(this).next('input').focus();
}
});
Is that what you're looking for? http://fiddle.jshell.net/C3jeY/

Linking HTML to JSP page

I'm have problems linking my HTML and JSP. Just trying to make a simple html page to take a "email" and "password" then have the jsp display on the next page. But when i click submit the parameters dont get passed i just get "null", any ideas where im going wrong?
HTML Page
<html>
<head>
<title>Enter your email and password</title>
</head>
<body>
<form action="form.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">Email:</td>
<td><input type="text" email=email></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="text" password=password></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
JSP Page
<html>
<body>
<%
// get info from request
String email = request.getParameter("email");
String password = request.getParameter("password");
%>
<p>Here is the info you provided:</p>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">Email:</td>
<td><%= email %></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><%= password %></td>
</tr>
</table>
<form action= "Next" method="post">
<input type="submit" value="Return">
</form>
</body>
</html>
You need to use the name attribute in the form fields .
<input type="text" name="email"></td>
The value of "email" can be retrieved in JSP as :
String email = request.getParameter("email");
OR
${param.email}
OR
<c:out value="${param.email}"/> <%--(Using JSTL)--%>
Here are the complete list of attributes.
name = string #
The name part of the name/value pair associated with this element for the purposes of form submission.
You should specify the input name attribute. Try this,
<form action="form.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">Email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
For more info you can try this link

Categories