Spring Struts集成
Spring框架提供了一种管理依赖项的简便方法。它可以很容易地与struts 2框架集成。
ContextLoaderListener 类用于与Struts 2通信Spring应用程序。必须在web.xml文件中指定。
您需要执行以下步骤:
创建struts2应用程序并添加spring jar文件。
在 web.xml 文件中,定义ContextLoaderListener类。
在 struts.xml 文件中,为操作类定义bean名称。
在 applicationContext.xml 文件中,创建Bean。其类别名称应为动作类别名称,例如com.lidihuo.Login和id应该与struts.xml文件的操作类(例如login)匹配。
在 action类中,定义其他属性,例如消息。
Spring和Struts 2集成示例
您需要创建以下文件以简化操作spring and struts 2应用程序:
index.jsp
web.xml
struts.xml
applicationContext.xml
Login.java
welcome.jsp
error.jsp
1)index.jsp
此页面从用户那里获取名称。
<%@ 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 侦听器类定义了控制器类,以在struts2和spring应用程序之间建立连接。
<?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
它定义包含操作和结果的程序包。在这里,动作类名称是login,将在applicationContext.xml文件中进行搜索。
<?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
它定义了一个具有ID登录名的bean。该bean对应于mypack.Login类。
它应该位于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
它定义了两个属性userName和具有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
这是错误页面。但这不是必需的,因为我们没有在动作类的execute方法中定义任何逻辑。
输出