JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Struts 2 Spring Integration
  • Struts 2 and Spring Integration can be done through  struts2-spring-plugin.
  • By doing this we can utilize the Spring's powerful Dependency Injection feature.

First we need to  add the ContextLoaderListener to the web.xml file as shown below.

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/SpringConfiguration.xml</param-value>
    </context-param>

<listener>

    <listener-class>org.springframework.web.context.
ContextLoaderListener</listener-class>
</listener>


Springconfiguration.xml file will be used for doing the Spring bean configuration.

<?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="helloWorld" class="myaction.HelloWorld" >
 <property name="message" value="Hello World!" />
 </bean>
</beans>

Here we have registered the HelloWorld class and injected the "Hello World!"
message to the message attribute using the setter injection method.

All the Struts 2 action configuration goes in the struts.xml file.

<!DOCTYPE struts PUBLIC "-//Apache Software
Foundation//DTD Struts

Configuration 2.0//EN" "http://struts.apache.
org/dtds/struts-2.0.dtd"
>

<struts>
 <package name="default" extends="struts-default">
 <action name="helloWorld" class="helloWorld">
 <result name="success">/success.jsp</result>
 </action>
 </package>
</struts>

The only change here is instead of referring the myaction.HelloWorld class directly,
we relate it using the bean name given in the spring bean configuration file

The HelloWorld class is shown below. In the execute() method we simply return
"SUCCESS" and the message attribute is set using setter injection.


package myaction;
public class 
HelloWorld {

 private String message;

 public String getMessage() {
 return message;
}

 public void setMessage(String message) {
 this.message = message;
 }
 public String execute() {
 return "SUCCESS";
 }
}

In the index.jsp page we forward the request to the helloWorld action.

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<body>
<h3><s:property value="message"/></h3>
.......<b><s:property value="name"/></b>
</body>
</html>

index.jsp


<%@taglib uri="/struts-tags" prefix="s" %>
<html> 
    <body>
       
        <s:form action="helloWorld">
        
        <s:textfield name="name" lable="Name"/>
          <s:submit value="click"/>
        </s:form>

</body>
</html>


Neccessary Jar file which we need is as follow:



Output::-





Download Source without lib
Download War with lib