Spring Struts集成
spring struts集成
spring框架提供了一种管理依赖项的简便方法。它可以很容易地与struts 2框架集成。
contextloaderlistener 类用于与struts 2通信spring应用程序。必须在web.xml文件中指定。
您需要执行以下步骤:
- 创建struts2应用程序并添加spring jar文件。
- 在 web.xml 文件中,定义contextloaderlistener类。
- 在 struts.xml 文件中,为操作类定义bean名称。
- 在 applicationcontext.xml 文件中,创建bean。其类别名称应为动作类别名称,例如com.yapf.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方法中定义任何逻辑。
sorry!
输出



