There are two ways to configuring spring in struts.
1. Using org.springframework.web.struts.ActionSupportAction
class.
2. Using
org.springframework.web.struts.DelegatingActionProxy.
Lets check second
method:- 2. Using
org.springframework.web.struts.DelegatingActionProxy.
DelegatingActionProxy is
a proxy class for appropriate Action.
Here we extends strut’s
Action class to write action class.
But we dont mention this
action class in struts-config.xml file.
Instead of our action
class we use DelegatingActionProxy.
1. Modify
struts-config.xml to load spring’s context loder plugin.
Add following entries in
struts-xml
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property
property="contextConfigLocation"
value="/WEB-INF/ApplicationContext.xml"/>
</plug-in>
Here we are defining the
context file to read. This plugging in loaded when server starts and it
reads the configuration file(here its placed at
/WEB-INF/ApplicationContext.xml).
2. Modify
struts-config.xml file and change action mapping as:-
<action
path="/loginAction"
type="org.springframework.web.struts.DelegatingActionProxy"
name="loginForm"
scope="request"
validate="true"
input="/WEB-INF/pages/login.jsp">
<forward
name="success" path="/homePage.do"/>
<forward
name="failure" path="/login.do"/>
</action>
Here we are using
DelegatingActionProxy instead of LoginAction. All requests goes to
DelegatingActionProxy it is the responsibility of this class to call
appropriate action class.
2. Define bean
definitions in WEB-INF/ApplicationContext.xml.
<?xml
version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="businessDelegateBean"
class="my.login.action.BusinessDelegate">
</bean>
<bean
name="/loginAction" class="my.login.action.LoginAction">
<property
name="businessDelegate">
<ref
bean="businessDelegateBean"/>
</property>
</bean>
</beans>
Here i defined a bean
with id “businessDelegateBean” and map url pattern to actual action class.
DelegatingActionProxy class read this file and see that /loginAction is mapped
to LoginAction and it forward request to LoginAction, before that it inject
businessDelegate.
3. Define
BusinessDelegate class.
package my.login.action;
public class BusinessDelegate {
public String validateUser(String userName,
String password) {
if(userName.equals("vivek")
&& password.equals("kumar")) {
return "success";
}
return "failure";
}
}
4. Modify action class.
package my.login.action;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import my.login.form.LoginForm;
import
org.apache.struts.action.ActionError;
import
org.apache.struts.action.ActionErrors;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.springframework.web.struts.ActionSupport;
public class LoginAction
extends ActionSupport {
private BusinessDelegate businessDelegate;
public BusinessDelegate getBusinessDelegate()
{
return businessDelegate;
}
public void setBusinessDelegate(BusinessDelegate
businessDelegate) {
this.businessDelegate = businessDelegate;
}
@Override
public ActionForward execute(ActionMapping
mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
LoginForm loginForm =(LoginForm) form;
String target =
businessDelegate.validateUser(loginForm.getUserName(),
loginForm.getPassword());
if("failure".equalsIgnoreCase(target))
{
ActionErrors errors = new
ActionErrors();
errors.add("loginError", new
ActionError("login.failure.message"));
saveErrors(request,errors);
}
return mapping.findForward(target);
}
}
No comments:
Post a Comment