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 In the file, define the ContextLoaderListener class. in struts.xml In the file, define the bean name for the action class. in applicationContext.xml In the file, create a Bean. The class name should be the name of the action class, such as com.w3codebox.Login and id should match the action class (e.g., login) in the struts.xml file. in Action classIn addition, define other properties, such as messages.
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.
<%@tagliburi="/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 struts2und establishes 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
Es definiert ein Paket, das Operationen und Ergebnisse enthält. Hier ist der Name der Aktionsklasse login, der im Datei applicationContext.xml gesucht wird.
<?xml version="1.0" encoding="UTF-8" ?> !DOCTYPE struts public "-//Apache Software Foundation//DTD Struts Konfiguration 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
Es definiert einen Bean mit dem ID Login-Namen. Der Bean entspricht der Klasse mypack.Login.
Es sollte sich im WEB-INF-Verzeichnis.
<?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>
5)Login.java
EsdefiniertzweiAttributeuserNameundeineNachrichtmitdemexecute-Methode,dieerfolgreichzurückgibt.
packagemypack; publicclassLogin{ privateStringuserName,message; publicStringgetMessage(){ returnmessage; } publicvoidsetMessage(Stringmessage){ this.message=message; } publicStringgetUserName(){ returnuserName; } publicvoidsetUserName(StringuserName){ this.userName=userName; } publicStringexecute(){ return"success"; } }
6)welcome.jsp
EszeigtdenWertderAttributwerteuserNameundNachricht.
<%@tagliburi="/struts-tags"prefix="s"%> Willkommen,<s:propertyvalue="userName"/><br/> ${message}
7)error.jsp
Diesist eineFehlerseite,aber das ist nichtnotwendig,weilwirkeinenLogikin derexecute-Methode derAktionklassedefinierthaben.
Entschuldigung!
Ausgabe