Can anyone spot why the file (Http.Part) Variable is null after submitting the form?
Controller Code
#RequestMapping(value="/account/update", method=RequestMethod.POST)
public String addImage(Account account, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest, RedirectAttributes redirectAttributes,
Locale locale,HttpServletRequest request, #RequestParam(value="file", required=false) Part file){
if(file==null){
uiModel.addAttribute("file_error", "File upload failure");
}
View
<spring:url value="/account/update" var="update" />
<form:form modelAttribute="account" action="${update}" method="POST" enctype="multipart/form-data" >
<c:if test="${file_error != null}">
Errors: ${file_error }
</c:if><br />
<label for="file">
<spring:message code="account.upload.file"/>
</label>
<input name="file" type="file"/>
<input type="submit" value="Save" />
</form:form>
Have you add a multipartResolver?
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000"/>
</bean>
Related
After uploading a excel file in Spring MVC Application, getting following error
HTTP Status 400 - Parameter conditions "" OR "" OR "" not met for actual request parameters
uses the following 3 methods definitions:
#RequestMapping(value="/uploadExcelFile",params="filep1",method=RequestMethod.POST)
public String uploadFiles(#RequestParam("filep1") CommonsMultipartFile file,
Model p_objModel,
HttpSession l_objHttpSession) throws IOException
#RequestMapping(value="/uploadExcelFile",params="filep2",method=RequestMethod.POST)
public String uploadFiles2(
Model p_objModel,
HttpSession l_objHttpSession,MultipartFile file) throws IOException
#RequestMapping(value="/uploadExcelFile",params = "filep3",method=RequestMethod.POST)
public ModelAndView upload(#RequestParam CommonsMultipartFile file,HttpSession session){
String path=session.getServletContext().getRealPath("/");
In JSP file configurations is as follows:
<form action="uploadExcelFile" method="post" enctype="multipart/form-data">
<p>Custom file:</p>
<input type="file" class="custom-file-input" id="customFile" name="filename">
<label class="custom-file-label" for="customFile">Choose file to upload</label>
</div> -->
<input type="file" name="filep1" id="myFile1">
<input type="file" name="filep2" id="myFile2">
<input type="file" name="filep3" id="myFile3">
<div class="mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
spring-servlet.xml configuration:
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="10485760"></beans:property>
</beans:bean>
Please help out on this.
Thanks in Advance!
I am trying to implement one simple user authentication by Spring Security in Spring MVC model but I am constantly getting "Bad credentials" even if I am providing correct credentials .
Below are my files. Can someone please suggest what am I missing?
Security.xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/security
https://www.springframework.org/security/spring-security-4.2.xsd">
<http>
<csrf disabled="true"/>
<intercept-url pattern="/login*" access="ROLE_USER"/>
<form-login login-page="/" default-target-url="/login" authentication-failure-url="/"/>
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="rishavraj#gmail.com" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
Controller.java:
#RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView Login(#RequestParam Map<String, String> reqvar) {
System.out.println("login");
ModelAndView form = new ModelAndView("Index");
return form;
}
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView Index() {
System.out.println("Index");
ModelAndView form = new ModelAndView("Index");
return form;
}
Index.jsp:
<c:if test="${SPRING_SECURITY_LAST_EXCEPTION !=null}">
<div class="alert alert-danger fade in">
×
<strong> Wrong Email or Password </strong>
<h4> Caused by :
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
</h4>
</div>
</c:if>
<form action="/Mail/login" method="post">
<div class="form-group">
<label for="inputUserName">Email</label>
<input class="form-control" placeholder="Email ID" type="text" id="inputUserName" name="Email"/>
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input class="form-control" placeholder="Login Password" type="password" id="inputPassword" name="password"/>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Login</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>`
</div>
</form>
Trying Kendo UI JSP editable grid. Grid is working with few problems.
(Version : Kendo UI v2015.3.1111 )
Export: Even with allPages="allPages", its exporting only current
page.
After CREATE, GRID is not updated with server response which has user createDate. Same with Update, grid not updated with update date
even though the updated user object is passed.
Grid shows user added even if it failed in the backend. How to handle error response for create /update and show the failed message ?
Any help greatly appreciated.
Controller create part:
#RequestMapping(value = "/user/create", method = RequestMethod.POST)
public #ResponseBody User create(#RequestBody Map<String, Object> model) {
log.debug("create");
User target = new User();
target.setUserName((String)model.get("UserName"));
target.setFirstName((String)model.get("firstName"));
target.setLastName((String)model.get("lastName"));
target.setOpenDate(getDateFromStr((String)model.get("openDate")));
target.setEditDate(getDateFromStr((String)model.get("editDate")));
User user = userDao.createUser(target);
log.info("user"+user.getUserId()+user.getOpenDate());
return user;
}
JSP PART:
<c:url value="/user/create" var="createUrl" />
<c:url value="/user/read" var="readUrl" />
<c:url value="/user/update" var="updateUrl" />
<c:url value="/user/destroy" var="destroyUrl" />
<c:url value="/user/saveexcel" var="saveExcelUrl" />
<c:url value="/user/savepdf" var="savePdfUrl" />
<kendo:grid name="grid" pageable="true" sortable="true" height="750px" filterable="true">
<kendo:grid-scrollable/>
<kendo:grid-pdf fileName="KendoUIGridExport.pdf" allPages="allPages" proxyURL="${savePdfUrl}"/>
<kendo:grid-excel fileName="KendoUIGridExport.xlsx" allPages="allPages" proxyURL="${saveExcelUrl}" />
<kendo:grid-editable mode="popup" confirmation="Are you sure you want to remove this item?"/>
<kendo:grid-toolbar>
<kendo:grid-toolbarItem name="create"/>
<kendo:grid-toolbarItem name="excel"/>
<kendo:grid-toolbarItem name="pdf"/>
</kendo:grid-toolbar>
<kendo:grid-columns>
<kendo:grid-column title="User Name" field="userName" width="120px"/>
<kendo:grid-column title="First Name" field="firstName" width="120px" />
<kendo:grid-column title="Last Name" field="lastName" width="120px" />
<kendo:grid-column title="Open Date" field="openDate" width="120px" format="{0:MM/dd/yyyy}" />
<kendo:grid-column title="Edit Date" field="editDate" width="120px" format="{0:MM/dd/yyyy}" />
<kendo:grid-column title=" " width="150px">
<kendo:grid-column-command>
<kendo:grid-column-commandItem name="edit" />
<kendo:grid-column-commandItem name="destroy" />
</kendo:grid-column-command>
</kendo:grid-column>
</kendo:grid-columns>
<kendo:dataSource pageSize="10" serverPaging="false" serverSorting="false" serverFiltering="false" serverGrouping="false" >
<kendo:dataSource-transport>
<kendo:dataSource-transport-create url="${createUrl}" type="POST" dataType="json" contentType="application/json"/>
<kendo:dataSource-transport-read url="${readUrl}" type="POST" dataType="json" contentType="application/json"/>
<kendo:dataSource-transport-update url="${updateUrl}" type="POST" dataType="json" contentType="application/json" />
<kendo:dataSource-transport-destroy url="${destroyUrl}" type="POST" dataType="json" contentType="application/json" />
<kendo:dataSource-transport-parameterMap>
<script>
function parameterMap(options,type) {
return JSON.stringify(options);
}
</script>
</kendo:dataSource-transport-parameterMap>
</kendo:dataSource-transport>
<kendo:dataSource-schema>
<kendo:dataSource-schema-model id="userId">
<kendo:dataSource-schema-model-fields>
<kendo:dataSource-schema-model-field name="userName" type="string" >
<kendo:dataSource-schema-model-field-validation required="true" />
</kendo:dataSource-schema-model-field>
<kendo:dataSource-schema-model-field name="firstName" type="string">
<kendo:dataSource-schema-model-field-validation required="true" />
</kendo:dataSource-schema-model-field>
<kendo:dataSource-schema-model-field name="lastName" type="string">
<kendo:dataSource-schema-model-field-validation required="true" />
</kendo:dataSource-schema-model-field>
<kendo:dataSource-schema-model-field name="openDate" type="date" editable="false" />
<kendo:dataSource-schema-model-field name="editDate" type="date" editable="false"/>
</kendo:dataSource-schema-model-fields>
</kendo:dataSource-schema-model>
</kendo:dataSource-schema>
</kendo:dataSource>
</kendo:grid>
Resolved myself: For pdf, allPages="true" works.
Grid refresh: FOR requestEnd event
function onRequestEnd(e) {
if (e.type == "create") {
e.sender.read();
}
else if (e.type == "update") {
e.sender.read();
}
}
In a Spring MVC 3.1 application I'm trying to implement a remember-me feature (with info saved in the database).
Here's what I currently have :
I created a persistent_logins table.
I have this in my security context file :
<form-login login-page="/login"
authentication-failure-url="/login?err=true"
default-target-url="/"
username-parameter="username"
password-parameter="password"
login-processing-url="/validatelogin" />
<remember-me key="some_random_key"
token-validity-seconds="31536000"
data-source-ref="dataSource" />
In my login.jsp, I have :
<form action="/validatelogin" method="post">
username : <input type='text' id='username' name='username' value='${SPRING_SECURITY_LAST_EXCEPTION.authentication.principal}' />
<br />
password : <input type='password' id='password' name='password' />
<br /><br />
remember me : <input type="checkbox" name="_spring_security_remember_me" />
<br /><br />
<input type="submit" value="submit" />
</form>
In the "<form-login>" bean, I've been able to rename the default "j_password" and "j_username" fields that have to be used in the jsp, using username-parameter and password-parameter. But I don't find a way to rename the "_spring_security_remember_me" checkbox field.
Any idea on how to rename it?
That is set using the parameter property of RememberMeServices.
Sadly this isn't settable using the namespace config. Here are a couple of ways you could set it:
Create a custom RememberMeServices and use it using <remember-me services-ref="myRememberMeServices">. Set the property on your bean.
Use a BeanPostProcessor (see 1.8) to set the property on the default RememberMeServices.
You should use BeanPostProcessor to set correct property:
Let's consider that you want to call your property "myRememberMeProperty"
Then your code should look like this:
public class MyBeanPostProcessor implements BeanPostProcessor {
String myRememberMeProperty;
public Object postProcessAfterInitialization(Object bean, String name) {
if (bean instanceof AbstractRememberMeServices) {
AbstractRememberMeServices rememberMe = (AbstractRememberMeServices) bean;
rememberMe.setParameter(getMyRememberMeProperty());
}
return bean;
}
public Object postProcessBeforeInitialization(Object bean, String name) {
return bean;
}
public void setMyRememberMeProperty(String myRememberMeProperty){
this.myRememberMeProperty = myRememberMeProperty;
}
public String getMyRememberMeProperty(){
return this.myRememberMeProperty;
}
}
<bean id="myBeanPostProcessor"
class="x.y.z.MyBeanPostProcessor">
<property name="myRememberMeProperty" value="myRememberMeProperty" />
</bean>
Hope it helps.
public class Parent {
private Integer Id;
private String name;
private String age;
private List<Childrens> childrens;
// getters and setters
}
My JSP classs:
<spring:bind path="parent.*">
<c:if test="${not empty status.errorMessages}">
<div class="error">
<c:forEach var="error" items="${status.errorMessages}">
<img src="<c:url value="/images/iconWarning.gif"/>"
alt="<fmt:message key="icon.warning"/>" class="icon" />
<c:out value="${error}" escapeXml="false" />
<br />
</c:forEach>
</div>
</c:if>
<form:form commandName="parent" method="post"
action="parent .html" onsubmit="return onFormSubmit(this)"
id="parentForm">
<appfuse:label styleClass="desc" key="name" />
<form:errors path="name" cssClass="fieldError" />
<form:input path="name" id="name" cssClass="text medium"
readonly="true" cssErrorClass="text medium error" />
<appfuse:label styleClass="desc" key="age" />
<form:errors path="age" cssClass="fieldError" />
<form:input path="age" id="cage" cssClass="text medium"
readonly="true" cssErrorClass="text medium error" />
I want show list of Childrens as a table. How can I do it? A list inside form page.
Probably your best bet is to use the built-in jstl tag lib (which you already) to declare the bean and get the list of children and iterate over them with "forEach".
Using spring form tag doesn't prevent you from doing this as far as I know.