现在的位置: 首页 > 综合 > 正文

【topic】 Struts 2 Spring Integration

2019年05月14日 ⁄ 综合 ⁄ 共 6574字 ⁄ 字号 评论关闭

学习Struts2框架过程中的一个工作草图,对于理解使用这些框架的朋友来说一定见笑了,只是可能对那些需要有较好的SSH框架例子以学习的朋友有所帮助。

链接见 http://www.vaannila.com/struts-2/struts-2-example/struts-2-spring-plugin-integration-1.html

 

In this simple hello world example you will seehow tointegrate Spring and Struts 2 using the
struts2-spring-plugin.
Bydoing this you can utilize the Spring's powerful Dependency Injection feature.
To learn more aboutDependency Injection refer this 
example.

 

First add the org.springframework.web.context.ContextLoaderListener to
the web.xmlfile

首先要把 org.springframework.web.context.ContextLoaderListener加入到
web.xml
文件中来

 

Web.xml

--------------

 

<?xmlversion="1.0" encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"
version="2.5">

 <display-name>Struts2Example14</display-name>

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

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

 

By default the applicationContext.xml file
will beused for doing the Spring bean configuration.

 

默认的 applicationContext.xml会用于对Spring
bean
配置文件进行配置。

 

<beans>

    <bean id="helloWorldClass"class="com.vaannila.HelloWorld" >

           <propertyname="message" value="Hello World!" />

    </bean>

</beans>

 

(一个bean有哪些属性

 

As you can see we have registered the HelloWorld class
andinjected the "
Hello World!" message to the message attribute
usingthe setter injection method.

从上可见,已经注册了HelloWorld 类,并把
"
HelloWorld!"消息注入到消息属性使用setter注入方法。

 

(注意在applicationContext.xml通过bean的名字helloWorldClass来注册com.vaannila.HelloWorld,这个注册的名字helloWorldClass又在struts.xml中被名为helloWorld的action配置引用,是说即中间做了一层解耦)

 

 

All the Struts 2 action configuration goes in the struts.xml file.

所有的Struts2action配置都在struts.xml文件中进行。

 

<struts>

    <package name="default"extends="struts-default">

       
<action name="helloWorld" class="helloWorldClass">

           
<resultname="SUCCESS">/success.jsp</result>

       
</action>

    </package>

</struts>

 

HelloWorld

------

public class
HelloWorld
{

 

privateString message;

 

publicString getMessage() {

returnmessage;

}

 

publicvoid setMessage(String message) {

this.message= message;

}

 

publicString execute() {

       
return "SUCCESS";

    }

}

 

HelloWorld类简单的执行execute方法,返回SUCCESS。

 

In the index.jsp page
we forward the request to the 
helloWorld action.

index.jsp中,把请求转递给helloWorld action

 

<METAHTTP-EQUIV="Refresh" CONTENT="0;URL=helloWorld.action">

 

所需的JAR包 见 (图略)

 

 

war文件包包含tomcat发布目录下,结果如下(图略)


似乎没有注入。

是应为message没有传递过来,应是
helloWorld.action
没有进行配置之故。

 

ECLIPSE中进行调试

(图略)

 

success.jsp的内容原来是

 

<body>

${message}

</body>

 

若更改为

 

<body>

hello world!

</body>

 

 

如下所示,说明了实际上已经由action转到了success.jsp
(图略)


但是

${message} 没有调用是什么原因?

 

这个问题是由于对JSP页面传值的机制没有认识所致,

下载后的文件success.jsp

 

<%@ pagelanguage="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type" content="text/html;charset=ISO-8859-1">

<title>HelloWorld</title>

</head>

<body>

${message}

</body>

</html>

 

这个文件中只是引用了${message},但是未对其进行定义,

 

需要在success.jsp页面中添加进来<%@
page isELIgnored=
"false"%>

然后声明要使用的变量<s:propertyvalue="message"/>

 

作了这些改变后,success.jsp如下

 

<%@ pagelanguage="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<%@ page isELIgnored="false"%>

<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type" content="text/html;charset=ISO-8859-1">

<title>HelloWorld</title>

</head>

<body>

<s:propertyvalue="message"/>

${message}

</body>

</html>

 

这时对注入器所在配置文件applicationContext.xml

 

<beans>

    <bean id="helloWorldClass"class="com.vaannila.HelloWorld" >

           <propertyname="message" value="changed" />

    </bean>

</beans>

 

对其bean 的property更改value(注入变量改变)

 

 然后更改就会显现出来(图略)。

 

当注入器注入改变后,需要重启tomcat才能给才能使结果更新显现(图略)

  

这个例子中,很容易体现出spring框架的依赖注入机制,但是struts框架的体现是在哪里呢

 

再来看一下web.xml

 

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"
version="2.5">

 <display-name>Struts2Example14</display-name>

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

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

 

对页面中地址的过滤器用到了struts2看到处理浏览器页这一层的传递动作机制的是名为struts2的过滤器。

这之中嵌套的listener是一个spring框架调用,是否从struts2角度而言,和其它框架的结合就放在
listener
中?

 

这个例子中的action处理机制是怎么样的

制作了“将其关联到spring bean配置文件中”的那么一个HelloWorld 类,

 

Success.JSP

---------------

<METAHTTP-EQUIV="Refresh" CONTENT="0;URL=helloWorld.action">

 

关于META标签

HTTP-EQUIV="Refresh"表示

Success.jsp只包含了元信息,可以认为是实现了到helloWorld.action的跳转,

是否一跳到*.action ,不断监听着的
spring
框架就接管工作了?

 

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

 

所有 Struts 2 action的配置都在 struts.xml Struts.xml中有个名为helloWorldaction

 

<action name="helloWorld" class="helloWorldClass">

<result name="SUCCESS">/success.jsp</result>

</action>

<action name="helloWorld" class="helloWorldClass">

<result name="SUCCESS">/success.jsp</result>

</action>

 

这段如何解释?

从输入index.jsp跳转到helloWorld.action, 引发了spring的侦听框架ContextLoaderListener唤醒spring开始工作?

进一步的, action引发了helloWorldClass类工作,由于helloWorldClass实际是在“对Spring
bean
配置文件进行配置”的applicationContext.xml中定义了

 

<beans>

    <bean id="helloWorldClass"class="com.vaannila.HelloWorld" >

           <propertyname="message" value="Hello World!" />

    </bean>

</beans>

 

所以引发helloWorldClass类,实际是引发了com.vaannila.HelloWorld类。

并将结果通过success.jsp进行回显。

 

<%@ page isELIgnored="false"%>        (isELIgnored标记是什么意思

<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type" content="text/html;charset=ISO-8859-1">

<title>HelloWorld</title>

</head>

<body>

<s:property value="message"/>(定义了一个变量message,其范围默认,是什么?又有哪几个范围?

${message}

</body>

</html>

未完结的 完

抱歉!评论已关闭.