Spring MVC - Basic
Spring Dispatcher Class in Web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
The
Dispatcher class as default look for dispacter-servlet.xml
To
Change the file name Add below in web.xml
contextConfigLocation
/WEB-INF/mvc-dispatcher-servlet.xml
setting view Resolver in the
DispacterServlet .xml
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property
name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property
name="suffix">
<value>.jsp</value>
</property>
</bean>
Controllers in Spring
AbstractController
protected
ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse
response) throws
Exception {
}
SimpleFormCotroller
setCommandClass(User.class);
- View Class (Form Class)
setCommandName("user");
FORM
JSP Tags – Tag Lib
<%@
taglib
uri="http://www.springframework.org/tags/form"
prefix="form"%>
<form:radiobutton
path="propertyName">
--- mapped to property in the view Class
To
hook Submit call
<input
type="submit">
protected
ModelAndView onSubmit(Object command) throws
Exception {
}
To
Set the Form view and success page
<bean
name="/welcome.htm"
class="controller.UserController"
p:formView="userForm"
p:successView="userSuccess"
/>
MultiActionController
<bean
name="/user/*.htm"
class="controller.CustomerController">
<property
name="methodNameResolver">
<bean
class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
<property
name="prefix"
value="test"
/>
<property
name="suffix"
value="Customer"
/>
</bean>
</property>
</bean>
InternalPathMethodNameResolver
Simple
implementation of MethodNameResolver that maps URL to method name.
Although this is the default implementation used by the
MultiActionController class (because it requires no configuration),
it's bit naive for most applications. In particular, we don't usually
want to tie URL to implementation methods.
methodNameResolver
– Resolves the method name with prefix & suffix
Sample
function
prefix-actionname(path)-suffix
public
ModelAndView testaddCustomer(HttpServletRequest request,
HttpServletResponse
response) throws
Exception {
}
addCustomer
– add.htm
-------------------------------------------------
ParameterMethodNameResolver - Simple implementation of MethodNameResolver that looks for a parameter value containing the name of the method to invoke.
name="methodNameResolver"> class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> name="paramName" value="action"/>
public
ModelAndView add(HttpServletRequest request,
HttpServletResponse
response) throws
Exception {
}
addCustomer
– customer.htm?action=add
PropertiesMethodNameResolver
The
most sophisticated and useful framework implementation of the
MethodNameResolver interface. Uses java.util.Properties defining the
mapping between the URL of incoming requests and method name. Such
properties can be held in an XML document.
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> name="mappings"> key="/customer/a.htm">add
method
name = add
SimpleUrlHandlerMapping
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> name="mappings"> key="/welcome.htm">welcomeController
No comments:
Post a Comment