multi action controller

7

Upload: thirugnanam-kandasamy

Post on 10-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

8/8/2019 Multi Action Controller

http://slidepdf.com/reader/full/multi-action-controller 1/7

8/8/2019 Multi Action Controller

http://slidepdf.com/reader/full/multi-action-controller 2/7

8/8/2019 Multi Action Controller

http://slidepdf.com/reader/full/multi-action-controller 3/7

8/8/2019 Multi Action Controller

http://slidepdf.com/reader/full/multi-action-controller 4/7

 MultiActionController with Validation

Description: Spring supports a multiaction controller where the  

multiple actions on a form can be grouped to a single controller. The

multiaction controller supports the mapping of requests to method

names which are similar to what Dispatch Action of struts do and it

does a bit more than that in terms of delegation of the requests.

Problem Description: A Simple Login page with two buttons, which take 

user name and password as the inputs. The page has two buttons, authenticate and login as shown below and performs validation on click of the buttons.

Implementation:

Step 1: Configuring the spring main servlet (DispatcherServlet) in 

web.xmlIn the web.xml we have named the servlet as spring; hence the springcontext file is required to be named asspring-servlet.xml 

8/8/2019 Multi Action Controller

http://slidepdf.com/reader/full/multi-action-controller 5/7

 Step 2: Configuring the LoginController(MultiActionController) and 

the LoginValidator inspring-servlet.xml The LoginController has the two setter methods

setMethodNameResolver ± this identifies the method that is

mapped to a particular request, the MethodNameResolver takes

two values

o  paramName ± The hidden variable that we declare in the login.jsp page, whose value needs to be set as the nameof the method name in the controller.

o defaultMethodName ± The method name of the controller  that is executed when the page is loaded.

setValidators ± this identifies the set of vallidators that thecontroller can execute in our example LoginValidator Step3 : Creating the login page login.jsp 

8/8/2019 Multi Action Controller

http://slidepdf.com/reader/full/multi-action-controller 6/7

We have two buttons Authenticate and Register, which when clicked

invoke the javascript method setAction() which will set the value of 

the hidden variable by namemethodToCall which is configured in the

spring-servlet.xml as the paramName which identifies the name of the 

method of the controller class to be invoked.<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 

<%@taglib prefix="spring" uri="http://www.springframework.org/tags"  

%>

<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

<html>< body><scripttype="text/javascript">functionsetAction(action1){document.forms[0].methodToCall.value = action1;document.forms[0].submit();}</script><style>

.error { color: r ed; }</style><c:if test="${not empty errors}"><divclass="error"><c:forEachvar ="error" items="${errors}"><c:outvalue="${error}" escapeXml="false"/>< br /></c:forEach>

</div>

</c:if >

<form:formmethod="post" commandName="command" >

<tablewidth="95%" bgcolor ="f8f8ff" border ="0" cellspacing="0" 

cellpadding="5"> <tr ><tdalign="right" width="20%">UserId</td>

<tdwidth="20%">

<spring:bind path="command.userid">

<form:input path="userid"/>

</spring:bind>

</td><tdwidth="60%"><form:errors path="userid" cssClass="error"/></td></tr ><tr ><tdalign="right" width="20%">Password</td><tdwidth="20%"><spring:bind path="command.password" ><form:input path="password"/>

</spring:bind>

</td>

<tdwidth="60%">

<form:errors path="password" cssClass="error"/>

8/8/2019 Multi Action Controller

http://slidepdf.com/reader/full/multi-action-controller 7/7

</td>

</tr >

<tr >

<tdalign="right" width="20%" >

<inputtype="button" align="middle" value="Authenticate" onclick ="javascript:setAction('authenticateUser');"> </td><tdwidth="20%" ><inputtype="button" align="middle" value="Register" 

onclick ="javascript:setAction('registerUser');"> </td>

</tr ></table>< br ><inputtype="hidden" name="methodToCall" value="" />

</form:form>

</ body></html>