<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property >
</plug-in>
Code language: HTML, XML (xml)
Create an xml file called ApplicationContext.xml in WEB-INF directory. <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Business Objects -->
<bean id="businessDelegate"
class="net.viralpatel.struts.helloworld.business.BusinessDelegate">
</bean>
<!-- Actions Classes -->
<bean name="/login" class="net.viralpatel.struts.helloworld.action.LoginAction">
<property name="businessDelegate"><ref local="businessDelegate"/></property>
</bean>
</beans>
Code language: HTML, XML (xml)
Note that in this example we are using Spring framework to instantiate an object of BusinessDelegate class as well as LoginAction class and injecting this object of BusinnessDelegate in LoginAction. So create a package net.viralpatel.struts.helloworld.business and create a java file BusinessDelegate.java in it. package net.viralpatel.struts.helloworld.business;
public class BusinessDelegate {
public String validateUser(String userName, String password) {
if(userName.equals("admin") && password.equals("123")) {
return "success";
}
return "failure";
}
}
Code language: Java (java)
Also add a property for BusinessDelegate class in LoginAction and its getter and setter method. This property will get injected by Spring. Copy/Paste following code in your LoginAction.java file. package net.viralpatel.struts.helloworld.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.viralpatel.struts.helloworld.business.BusinessDelegate;
import net.viralpatel.struts.helloworld.form.LoginForm;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action {
private BusinessDelegate businessDelegate;
public BusinessDelegate getBusinessDeletage() {
return businessDelegate;
}
public void setBusinessDelegate(BusinessDelegate businessDeletage) {
this.businessDelegate = businessDeletage;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String target = null;
LoginForm loginForm = (LoginForm)form;
target = businessDelegate.validateUser(loginForm.getUserName(),
loginForm.getPassword());
return mapping.findForward(target);
}
}
Code language: Java (java)
Note that LoginAction class is also instantiate using Spring. Hence we will modify entry in struts-config.xml. <action-mappings>
<action path="/login" name="LoginForm" validate="true"
input="/index.jsp"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="success" path="/welcome.jsp" />
<forward name="failure" path="/index.jsp" />
</action>
</action-mappings>
Code language: HTML, XML (xml)
In type attribute, instead of LoginAction, we have used org.springframework.web.struts.DelegatingActionProxy. This is spring framework class that will delegate the call to action to specific class in ApplicationContext.xml file. Note that path=”/login” should match to name=”/login” entry that we made in ApplicationContext.xml file. And that’s it. We have just implemented Spring support to our struts application. You can download war file of struts-spring application with full source here. SOURCE CODE Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Thanks Viral... good article...
You welcome Suresh :)
http://localhost:8080/Struts-Spring/login.do;jsessionid=813C9C5F6B4228C9D4E9B70394AE52F1
Error :
The requested resource (Servlet action is not available) is not available.
Viral, its working with spring 1.1 but not woking with spring 2.x. what changes we have to made to work with spring 2.x
Hi Sandeep,
I am not sure what packages are missing in Spring 2.x. I will check it and update this tutorial after making it compatible with Spring 2.x.
Thanks
I've got the same problem with Spring 2.5.6 , i resolved by downloading spring-struts.jar.zip( 25 k) ( http://www.java2s.com/Code/JarDownload/spring-struts.jar.zip )
now it works!
Thanks Andrea...its working now after downloading the jar.
Very nice tutorial, practical and worked easily. Thanks for avoiding loads of theory and adding something real.
Trying to access different .css files, but getting No mapping found for HTTP request with URI
Hi Vennela, Can you paste here the code snippet of CSS inclusion? I am not able to understand the problem from above statement.