mkyong

mkyong

Software Engineer • Writer • Dad

20+ years hands-on, writing about Java, Spring, and AI. Every code example here is tested.

1,960 posts

Posts by mkyong

Problem While deploying JSF 2.0 web application to Tomcat 6.0.26, hits the “JSP version of the container is older than 2.1” exception and failed to start the Tomcat server. But the JSP api v2.1 is included in the project class path, why the Tomcat is still saying that JSP version is older than 2.1? <dependency> […]

Read more JSF 2.0 + Tomcat : It appears the JSP version of the container is older than 2.1…

Problem While deploying JSF 2.0 web application to Tomcat 6.0.26, hits following jstl class not found error. java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config … Caused by: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config … 18 more Solution By default, Tomcat container doesn’t contain any jstl library. To fix it, declares jstl.jar in your Maven pom.xml file. <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> Note Please refer […]

Read more java.lang.ClassNotFoundException : javax.servlet.jsp.jstl.core.Config

Problem In Eclipse IDE, while deploying a JSF 2.0 web application to Tomcat 6.0.26, hits the following exception and failed to start the Tomcat server. P.S Both jsf-api-2.1.0-b03.jar and jsf-impl-2.1.0-b03.jar libraries are included in the project classpath. INFO: Unsanitized stacktrace from failed start… java.lang.IllegalArgumentException: javax.faces.context.ExceptionHandlerFactory at javax.faces.FactoryFinder.validateFactoryName(FactoryFinder.java:630) at javax.faces.FactoryFinder.setFactory(FactoryFinder.java:287) … SEVERE: Critical error during deployment: […]

Read more java.lang.IllegalArgumentException: javax.faces.context.ExceptionHandlerFactory

This article shows a few ways to convert an java.io.InputStream to a String. Table of contents 1. ByteArrayOutputStream 2. InputStream#readAllBytes (Java 9) 3. InputStreamReader + StringBuilder 4. InputStreamReader + BufferedReader (modified line breaks) 5. Java 8 BufferedReader#lines (modified line breaks) 6. Apache Commons IO 7. Download Source Code 8. References What are modified line breaks? […]

Read more How to convert InputStream to String in Java

In Java, we can use ByteArrayInputStream to convert a String to an InputStream. String str = "mkyong.com"; InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)); Table of contents 1. ByteArrayInputStream 2. Apache Commons IO – IOUtils 3. Download Source Code 4. References 1. ByteArrayInputStream This example uses ByteArrayInputStream to convert a String to an InputStream and saves it […]

Read more How to convert String to InputStream in Java

In Java old days, it lacks of method to determine the free disk space on a partition. But this is changed since JDK 1.6 released, a few new methods – getTotalSpace(), getUsableSpace() and getFreeSpace(), are bundled with java.io.File to retrieve the partition or disk space detail. Example package com.mkyong; import java.io.File; public class DiskSpaceDetail { […]

Read more How to get free disk space in Java

A Java program to demonstrate the use of java.io.File setReadOnly() method to make a file read only. Since JDK 1.6, a new setWritable() method is provided to make a file to be writable again. Example package com.mkyong; import java.io.File; import java.io.IOException; public class FileReadAttribute { public static void main(String[] args) throws IOException { File file […]

Read more How to make a file read only in Java

A Java program to demonstrate the use of java.io.File isHidden() to check if a file is hidden. package com.mkyong; import java.io.File; import java.io.IOException; public class FileHidden { public static void main(String[] args) throws IOException { File file = new File("c:/hidden-file.txt"); if(file.isHidden()){ System.out.println("This file is hidden"); }else{ System.out.println("This file is not hidden"); } } } Note […]

Read more How to check if a file is hidden in Java

Problem Recently, just converted the Spring MVC xml-based form controller to annotation-based form controller, and hits the following error message. SEVERE: Neither BindingResult nor plain target object for bean name ‘customerForm’ available as request attribute java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘customerForm’ available as request attribute Above error message is clearly […]

Read more Spring MVC – Neither BindingResult nor plain target object for bean name ‘xxx’ available as request attribute.

Spring uses MultipartResolver interface to handle the file uploads in web application, two of the implementation : StandardServletMultipartResolver – Servlet 3.0 multipart request parsing. CommonsMultipartResolver – Classic commons-fileupload.jar Tools used in this article : Spring 4.3.5.RELEASE Maven 3 Tomcat 7 or 8, Jetty 9 or any Servlet 3.0 container In a nutshell, this article shows […]

Read more Spring MVC file upload example

Problem In Spring MVC application, while clicking on the file upload button, it hits the following property type conversion error? Failed to convert property value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [byte[]] for property file; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.web.multipart.commons.CommonsMultipartFile] to required type [byte] for property file[0]: PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned […]

Read more Spring MVC failed to convert property value in file upload form

In this tutorial, we show you how to develop a Spring MVC annotation-based MultiActionController, by using @RequestMapping. In XML-based MultiActionController, you have to configure the method name resolver (InternalPathMethodNameResolver, PropertiesMethodNameResolver or ParameterMethodNameResolver) to map the URL to a particular method name. But, life is more easier with annotation support, now you can use @RequestMapping annotation […]

Read more Spring MVC MultiActionController annotation example

Spring MVC comes with AbstractJExcelView class to export data to Excel file via JExcelAPI library. In this tutorial, it show the use of AbstractJExcelView class in Spring MVC application to export data to Excel file for download. 1. JExcelAPI Get the JExcelAPI library. <!– JExcelAPI library –> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.3</version> </dependency> 2. Controller A […]

Read more Spring MVC and Excel file via AbstractJExcelView

ParameterMethodNameResolver, a MultiActionController method name resolver to map URL to method name via request parameter name, and the parameter name is customizable through the “paramName” property. See following example : 1. MultiActionController A MultiActionController example. package com.mkyong.common.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class CustomerController extends MultiActionController{ public ModelAndView add(HttpServletRequest request, HttpServletResponse […]

Read more Spring MVC ParameterMethodNameResolver example

PropertiesMethodNameResolver, a flexible MultiActionController method name resolver, to define the mapping between the URL and method name explicitly. See following example : 1. MultiActionController A MultiActionController example. package com.mkyong.common.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class CustomerController extends MultiActionController{ public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception { return new ModelAndView("CustomerPage", "msg","add() […]

Read more Spring MVC PropertiesMethodNameResolver example

In Spring MVC application, MultiActionController is used to group related actions into a single controller, the method handler have to follow below signature : public (ModelAndView | Map | String | void) actionName( HttpServletRequest, HttpServletResponse [,HttpSession] [,CommandObject]); 1. MultiActionController See a MultiActionController example. package com.mkyong.common.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class […]

Read more Spring MVC MultiActionController example

In last Spring MVC form handling example, we should you the use of SimpleFormController to handle single page form submission, which is quite straightforward and easy. But, sometimes, you may need to deal with “wizard form“, which need handle form into multiple pages, and ask user to fill in the form page by page. The […]

Read more Spring MVC handling multipage forms with AbstractWizardFormController

Problem In Spring MVC application, often times, you may applying few view resolver strategies to resolve the view name. For example, combine three view resolvers together : InternalResourceViewResolver, ResourceBundleViewResolver and XmlViewResolver. <beans …> <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location"> <value>/WEB-INF/spring-views.xml</value> </property> </bean> <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="spring-views" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix"> <value>/WEB-INF/pages/</value> </property> […]

Read more Configure multiple view resolvers priority in Spring MVC

In Spring MVC, org.springframework.web.servlet.view.RedirectView, as name indicated, a view redirect to another absolute, context relative, or current request relative URL. In this tutorial, we show you a complete example to use RedirectView class. 1. RedirectView Declare a RedirectView bean, named “DummyRedirect“, redirect to URL “DummyRedirectPage.htm“. File : spring-views.xml <beans …> <!– Redirect view –> <bean […]

Read more Spring MVC RedirectView example

In last Spring MVC form handling example, if you refresh the form success view, most browsers will prompt a pop-up dialog to confirm about the form resubmission. If you click “yes”, the form will be resubmitted again, this scenario is well-known as duplicated form submission. Figure : example of duplicated form submission. The common solution […]

Read more Handling duplicate form submission in Spring MVC

This tutorial shows you how to do form handling in Spring Web MVC application. Technologies and tools used: Java 11 Spring 5.2.22.RELEASE JSP JSTL 1.2 Embedded Jetty Server 9.4.45.v20220203 Servlet API 4.0.4 Bootstrap 5.2.0 (webjars) Hibernate Validator 6.2.5.Final HSQLDB 2.7.0 IntelliJ IDEA Maven 3.8.6 Spring Test 5.2.22.RELEASE Hamcrest 2.2 JUnit 5.9 Table of contents: 1. […]

Read more Spring MVC form handling example

In Spring MVC, you can use <form:hidden /> to render a HTML hidden value field. For example, <form:hidden path="secretValue" /> It will render the following HTML code <input id="secretValue" name="secretValue" type="hidden" value="I’m hidden value"/> P.S Assume “secretValue” property contains value “I’m hidden value”. In this tutorial, we show you how to use Spring’s form tag […]

Read more Spring MVC hidden value example

In Spring MVC, the field error messages are generated by validators associated with the controller, and you can use the <form:errors /> tag to render those field error messages in an default HTML “span” tag. For example, 1. Validator A validator to check the “username” field, if empty, return the “required.username” error message from the […]

Read more Spring MVC form errors tag example

In Spring MVC, form tags – <form:select />, <form:option /> or <form:options />, are used to render HTML dropdown box. See following examples : //SimpleFormController protected Map referenceData(HttpServletRequest request) throws Exception { Map referenceData = new HashMap(); Map<String,String> country = new LinkedHashMap<String,String>(); country.put("US", "United Stated"); country.put("CHINA", "China"); country.put("SG", "Singapore"); country.put("MY", "Malaysia"); referenceData.put("countryList", country); } 1. […]

Read more Spring MVC dropdown box example

In Spring MVC, use <form:textarea /> to render a HTML textarea field. For example, <form:textarea path="address" rows="5" cols="30" /> It will render the following HTML code <textarea id="address" name="address" rows="5" cols="30"></textarea> In this tutorial, we show you how to use Spring’s form tag “textarea” to render a HTML textarea to store the “address“. Additionally, add […]

Read more Spring MVC textarea example

In Spring MVC, <form:checkbox /> is used to render a HTML checkbox field, the checkbox values are hard-coded inside the JSP page; While the <form:checkboxes /> is used to render multiple checkboxes, the checkbox values are generated at runtime. In this tutorial, we show you 3 different ways of render HTML checkbox fields: 1. <form:checkbox […]

Read more Spring MVC checkbox and checkboxes example

In Spring MVC, you can use <form:password /> tag to render a HTML password field. For example, <form:password path="password" /> It will renders following HTML code <input id="password" name="password" type="password" value=""/> Note In Spring’s documentation, it mention about the ‘showPassword‘ attribute will display the password value, but it’s failed in my testing, may be you […]

Read more Spring MVC password example

In Spring MVC, you can use <form:input /> tag to render a HTML textbox field. For example, <form:input path="userName" /> It will renders following HTML code <input id="userName" name="userName" type="text" value=""/> In this tutorial, we show you how to use Spring’s form tag “input” to render a HTML textbox to store the “userName“. Additionally, add […]

Read more Spring MVC textbox example

In general, to return a view or page in Spring MVC application, you need to create a class, which extends the AbstractController , and return a ModelAndView() object. public class WelcomeController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView model = new ModelAndView("WelcomePage"); return model; } } In the bean […]

Read more Spring MVC ParameterizableViewController example

In J2EE / servlet web application, you can map error page to specify exception like this : web.xml <error-page> <error-code>404</error-code> <location>/WEB-INF/pages/404.jsp</location> </error-page> <error-page> <exception-type>com.mkyong.web.exception.CustomException</exception-type> <location>/WEB-INF/pages/error/custom_error.jsp</location> </error-page> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/WEB-INF/pages/generic_error.jsp</location> </error-page> The above code should be self-exploratory. If the exception handling function exists in the servlet container, why we still need to use the Spring to […]

Read more Spring MVC Exception Handling Example

Problem In Spring MVC application, the 404 error code is configured properly. See the following web.xml snippet. File : web.xml <web-app …> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> //… <error-page> <error-code>404</error-code> <location>/WEB-INF/pages/404.htm</location> </error-page> </web-app> However, when user access any non-exist resources, it will display a blank page instead of the 404.htm. Solution […]

Read more 404 error code is not working in Spring MVC

In Spring MVC, ResourceBundleViewResolver is used to resolve “view named” based on view beans in “.properties” file. By default, ResourceBundleViewResolver will loads the view beans from file views.properties, which located at the root of the project class path. However, this location can be overridden through the “basename” property, for example, <beans …> <bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property […]

Read more Spring MVC ResourceBundleViewResolver example