Notes by Fang
Displaying notes 21 - 30
Created by Fang on March 30, 2012 15:12:14
Last update: March 30, 2012 15:12:14
I tested this on Spring MVC 3.1.
<?xml version="1.0" encoding="UTF-8"?>
<beans x...
Created by Fang on March 30, 2012 15:04:04
Last update: March 30, 2012 15:04:04
Spring MVC 3.1 can send either JSON or HTML response on the same URL, depending on the type of response requested. With this mechanism, a page can be sent when directly requested from a link, but a JSON response can be sent in response to an AJAX request. This is the controller code:
package com.example; import java.util.Map; ... In the above example, JSON response will be sent when the HTTP request contains header "Accept: application/json". HTML response will be sent then the header is "Accept: */*", or "Accept: text/html", or anything else. You can add a limitation that the HTML response does not produce "application/json". But then the question is which response will be sent when the HTTP header is "Accept: */*"? Both methods will...
Created by Fang on March 30, 2012 12:28:47
Last update: March 30, 2012 12:28:47
The HandlerMapping bean for @RequestMapping annotated @Controller is:
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping for Spring MVC prior to 3.1
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping for Spring MVC 3.1
This info might be handy if you want to add an interceptor:
<beans>
<bean id="handlerMapping"
...
Created by Fang on March 30, 2012 12:15:49
Last update: March 30, 2012 12:15:49
1. mvc:default-servlet-handler Configures a handler for serving static resources by forwarding to the Servlet container's default Servlet. Use of this handler allows using a "/" mapping with the DispatcherServlet while still utilizing the Servlet container to serve static resources. HandlerMapping: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Handler: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Attribute Description default-servlet-name The name of the default Servlet to forward to for static resource requests. The handler will try to auto-detect the container's default Servlet at startup time using a list of known names. If the default Servlet cannot be detected because of using an unknown container or because it has been manually configured, the servlet name must be set explicitly. 2. mvc:view-controller Defines a simple Controller that selects a view to render the response. HandlerMapping: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping Handler: org.springframework.web.servlet.mvc.ParameterizableViewController Attribute Description...
Created by Fang on March 30, 2012 10:23:21
Last update: March 30, 2012 10:23:21
These bean types are essential for the Spring MVC framework. I copied them here from the Spring Documentation for quick reference. Bean type Explanation HandlerMapping Maps incoming requests to handlers and a list of pre- and post-processors (handler interceptors) based on some criteria the details of which vary by HandlerMapping implementation. The most popular implementation supports annotated controllers but other implementations exists as well. HandlerAdapter Helps the DispatcherServlet to invoke a handler mapped to a request regardless of the handler is actually invoked. For example, invoking an annotated controller requires resolving various annotations. Thus the main purpose of a HandlerAdapter is to shield the DispatcherServlet from such details. HandlerExceptionResolver Maps exceptions to views also allowing for more complex exception handling code. ViewResolver Resolves logical String-based...
Created by Fang on March 28, 2012 12:30:22
Last update: March 28, 2012 12:30:36
To check password against login credential:
// import org.springframework.security.authenticat...
To replace authentication credentials with a new one:
SecurityContext securityContext = SecurityContextH...
Created by Fang on March 16, 2012 12:35:26
Last update: March 16, 2012 12:35:26
To programmatically resolve a spring message:
// import org.springframework.web.servlet.support....
Created by Fang on March 15, 2012 10:24:35
Last update: March 15, 2012 10:24:35
Suppose that I have an email field annotated with:
@NotEmpty(message="Please enter email address")
...
Bean validation will trigger two errors when no email address is entered:
the email field is empty
an empty email field is not a valid email address
Displaying both errors to the user with <form:errors> would be redundant and confusing:
<%@ taglib uri="http://www.springframework.org/tag...
This is how to display the first error only:
<spring:bind path="emailAddress">
<c:if test="$...
Created by Fang on March 06, 2012 14:38:52
Last update: March 06, 2012 15:56:45
This may or may not be useful, but I did the research so here's the code.
import org.springframework.beans.factory.annotatio...
Created by Fang on March 05, 2012 20:49:40
Last update: March 06, 2012 12:28:47
This is an easy to follow step-by-step tutorial to get started with Java JSR303 bean validation. It is solely focused on bean validation without dependency on any other Java technology. I hope that by following these steps you'll gain hands-on experience as well as some insights on how Java bean validation works.
Getting started with Java JSR 303 bean validation: the Maven project
Java JSR 303 bean validation: a simple example
JSR 303 cascade validation through inheritance
JSR 303 cascade validation through composition
JSR 303 validation with validation groups
JSR 303 bean validation *.List annotations