English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Spring framework provides a simple method for managing dependencies. It can easily be integrated with struts 2Framework integration.
ContextLoaderListener Class for communication with Struts 2Communication with the Spring application. It must be specified in the web.xml file.
You need to perform the following steps:
Create struts2Application and add the spring jar file. In web.xml Define the ContextLoaderListener class in the file. In struts.xml Define the bean name for the action class in the file. In applicationContext.xml Create a Bean in the file. The class name should be the action class name, such as com.w3codebox.Login and id should match the action class (e.g., login) in the struts.xml file. In Action ClassDefine other properties, such as messages, in
You need to create the following files to simplify the operation of spring and struts 2Application:
index.jsp web.xml struts.xml applicationContext.xml Login.java welcome.jsp error.jsp
1)index.jsp
This page retrieves the name from the user.
<%@ taglib uri="/struts-tags" prefix="s"%> <s:form action="login"> <s:textfield name="userName" label="UserName"></s:textfield> <s:submit></s:submit> </s:form>
2)web.xml
it provides for struts 2and ContextLoaderListener The listener class defines the controller class to be used in struts2Establish a connection between the application and the spring application.
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name <filter-class org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class </filter> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class </listener <filter-mapping> <filter-name>struts2</filter-name <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3)struts.xml
It defines a package containing operations and results. Here, the action class name is login, which will be searched in the applicationContext.xml file.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts public "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="abc" extends="struts-default"> <action name="login" class="login"> <result name="success">welcome.jsp</result> </action> </package> </struts>
4)applicationContext.xml
It defines a bean with the ID login name. The bean corresponds to the mypack.Login class.
It should be located in the WEB-INF directory.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance xmlns:p="http://www.springframework.org/schema/p xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="login" class="mypack.Login"> <property name="message" value="Welcome Spring"></property> </bean> </beans>
5Login.java
It defines two properties userName and a message with an execute method that returns success.
package mypack; public class Login { private String userName,message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String execute(){ return "success"; } }
6)welcome.jsp
It displays the values of the userName and message properties.
<%@ taglib uri="/struts-tags" prefix="s"%> Welcome, <s:property value="userName"/><br/> ${message}
7)error.jsp
This is an error page. But it is not necessary, because we have not defined any logic in the execute method of the action class.
Sorry!
Output