Monday, August 27, 2012

Struts - Servlet Basic

 Servelt - web.xml
---------------------
<servlet>  

<display-name> InitParamTest</display-name 
  <servlet-name> InitParamTest</servlet-name 
  <servlet-class  

test.InitParamTest
</servlet-class  

<init-param>

<description> test parameter</description>

<param-name> myname</param-name>
  <param-value> DummyValue</param-value

</init-param  

</servlet>


load-on-startup - servlet startup order while loading the web.xml

Filter - servlet

    A filter is an object that can transform a request or modify a response.
Filters are not servlets; they don't actually create a response.

They are preprocessors of the request before it reaches a servlet,
and/or postprocessors of the response leaving a servlet.

 * void init(FilterConfig config) throws ServletException: Called before the filter goes into service, and sets the filter's configuration object

 * void destroy(): Called after the filter has been taken out of service

 * void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException: Performs the actual filtering wor

filter-mapping - It is used to map with the particular Filter for particular url pattern


  

ServletConfig vs ServletContext
---------------------------------

ServletConfig has servlet wide scope. It is defined inside
in web.xml

ServletContext has application wide scope . It is defined outside the tag in web.xml

context parameters – available to the entire scope of the web application


- init parameters – available in the context of a servlet or filter in the web application
--------------------------------------------------------------------------------------------------------------

Struts Action Class
-------------------
1.ForwardAction class

2.DispatchAction class

    An abstract Action that dispatches to a public method that is
    named by the request parameter whose name is specified by the
    parameter property of the corresponding ActionMapping.
    This Action is useful for developers who prefer to combine
    many similar actions into a single Action class, in order to simplify their application design.

     To configure the use of this action in your struts-config.xml file, create an entry like this:

    <action path="/saveSubscription" type="org.apache.struts.actions.DispatchAction"
    name="subscriptionForm" scope="request" input="/subscription.jsp" parameter="method"/
   >

3.IncludeAction class

4.LookUpDispatchAction class
   
    It will dispatch to the particular public method based on a method getKeyMethodMap.

    This is useful in cases where an HTML form has multiple submit buttons.

    getKeyMethodMap - button key value is mapped to a function (hasmap button key value, function name)

5.MappingDispatchAction class

6.SwitchAction class

7.LocaleDispatchAction class

8.DownloadAction class


Multiple Reource files in struts
--------------------------------


  <message-resources parameter="resources.DefaultResource"/  >
  <message-resources parameter="resources.Registration" key="registrationBundle"/>

if both resources have same property (label.common.message), then can able to print

  <bean:message key="label.common.message" bundle="registrationBundle"/>


   

Spring MVC - Basic

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
key="/customer/b.htm">update key="/customer/c.htm">delete key="/customer/d.htm">list key="/customer/whatever.htm">add


method name = add


SimpleUrlHandlerMapping


 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            name="mappings">
                
                    key="/welcome.htm">welcomeController
key="/*/welcome.htm">welcomeController key="/helloGuest.htm">helloGuestController