Spring Struts集成

Spring框架提供了一種管理依賴項(xiàng)的簡(jiǎn)便方法。它可以很容易地與struts 2框架集成。

ContextLoaderListener 類用于與Struts 2通信Spring應(yīng)用程序。必須在web.xml文件中指定。


您需要執(zhí)行以下步驟:

創(chuàng)建struts2應(yīng)用程序并添加spring jar文件。 web.xml 文件中,定義ContextLoaderListener類。 struts.xml 文件中,為操作類定義bean名稱。 applicationContext.xml 文件中,創(chuàng)建Bean。其類別名稱應(yīng)為動(dòng)作類別名稱,例如com.nhooo.Login和id應(yīng)該與struts.xml文件的操作類(例如login)匹配。 action類中,定義其他屬性,例如消息。

Spring和Struts 2集成示例

您需要?jiǎng)?chuàng)建以下文件以簡(jiǎn)化操作spring and struts 2應(yīng)用程序:

index.jsp web.xml struts.xml applicationContext.xml Login.java welcome.jsp error.jsp

1)index.jsp

此頁(yè)面從用戶那里獲取名稱。

<%@ 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

它為struts 2和 ContextLoaderListener 偵聽(tīng)器類定義了控制器類,以在struts2和spring應(yīng)用程序之間建立連接。

<?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

它定義包含操作和結(jié)果的程序包。在這里,動(dòng)作類名稱是login,將在applicationContext.xml文件中進(jìn)行搜索。

<?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

它定義了一個(gè)具有ID登錄名的bean。該bean對(duì)應(yīng)于mypack.Login類。

它應(yīng)該位于WEB-INF目錄中。

<?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

它定義了兩個(gè)屬性u(píng)serName和具有execute方法的消息,其中返回成功。

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

它顯示userName和消息屬性的值。

<%@ taglib uri="/struts-tags" prefix="s"%>
Welcome, <s:property value="userName"/><br/>
${message}

7)error.jsp

這是錯(cuò)誤頁(yè)面。但這不是必需的,因?yàn)槲覀儧](méi)有在動(dòng)作類的execute方法中定義任何邏輯。

Sorry!

輸出

struts 2和spring集成1
彈簧和支撐2集成2

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清