工作流程与hello World!

本文详细介绍了Struts2的工作流程,从用户请求到视图展示的全过程。解析了Struts2框架如何处理请求,包括Filter、Interceptor、Action及Result等核心组件的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[align=center][size=large][b][color=blue]
struts2 工作流程与HelloWorld[/color][/b][/size][/align]

注:使用netbeans6.5调试

[color=blue][b].页面请求(jsp)——>逻辑控制器(Filter)——>拦截器(Interceotor)——>业务控制器(Action)——>返回处理器(Result) ——>返回视图[/b][/color]

1 浏览器中输入网址发出请求
2 web服务器(servlet)通过web.xl的配置,找到sturts过滤器,把以”.acton”结尾的请求,交给 struts过滤器处理。
3 struts过滤器寻找struts.xml配置文件——>调用struts拦截器——>调用sturts的action——>调用sturts的result——>返回视图。

[color=darkblue] 其中:1 网页与stuts2的数据交流是根根action中的定义的getter 、setter方法进行。这简化了使用。
2 servlet过滤器: servlet对某种规格的网址进行过滤,并交给符号专门接口的类进行处理。
3 网页与代码的交互:
当action类的变量有getter and setter方法时
代码方面:如果网页(html,jsp等)中的UI标签的name属性值就是变量名时,action类直接访问该变量就能得到网页中UI标签的数据。
网页方面:
(1) 如果是jsp,且具有struts2标签,并且网页中的UI标签的name属性值就是变量名时, 网页中的UI标签 直接就能得到该变量的数据。对于非UI的数据标签,有的是通过value值为变量名的方式,得到该变量的数据,这种标签有property
(2) 如果是纯jsp。则网页通过<%=request.getAttribute("变量名") %> 的方式,得到该变量的数据。
(3) 支持FreeMaker或veloctity的网页,如果该变量是一个类实例,则可通过${变量名.属性名}的方式得到该变量的属性值,如果不是一个实例,则可通过${变量名}的方式得到该变量的数据。
(4) 其中struts2中的jsp, 如果该变量是一个类实例,则可通过${变量名.属性名}的方式得到该变量的属性值,如果不是一个实例,则可通过${变量名}的方式得到该变量的数据.[/color]

例子见:网页与代码交互

用helloWorld中的例子说明:
1 输入网址 http://localhost:8084/Web2/helloxx.action
2 web服务器,找到FilterDispatcher过滤器,把该请求交给它。
3 FilterDispatcher根据struts配置,调用Hello.java程序(由于没有拉截器,也没有result,所以仅调用 这个action),执行其中的execute方法。根据约定,hello.java中的name,message变量将得到 发出网址中的网页(如果是由网页发出的请求,例中点hello.jsp中的“submit”按钮,将发出helloxx.action请求)得到值。
4 执行完毕,返回success字符串,FilterDispatcher要据这个字符串与struts.xml配置,调用hello.jsp页面。
5 根据约定,hello.jsp将调用名为Hello的action,取得name,message属性。
6 浏览器用户,将看到hello网页。

[size=large][color=blue] HelloWorld[/color][/size]下例具体使用Struts2。
第一步 配置WEB.XML--------Struts2过滤器
打开工程中的web.xml
输入:

<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



一个完整的web文件
<?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">

<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


第二步 建action
自建的action将继承struts2的ActionSupport(实现了检验功能)
覆盖execute方法
Hello.java
package hi;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Hello extends ActionSupport
{
public String message="";
public String name="";
public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


@Override
public String execute(){
message = "hello "+name+"!\n";
return "success";
}

}


第三步建struts.xml
该配置位于存位类的文件夹下(第0层)netbeans---为src/java
下面蓝色为固定的。红色的,extends 为“json-default”时,表示以json的形式组建数据,以用于ajax模式。这里是struts方式
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="hi" namespace="/" extends="struts-default">
<action name="helloxx" class="hi.Hello">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>



第四步,建hello.jsp
存于专放网页的文件夹下 (第0层) netbeans---为web
蓝色为使用了struts2标签的固定格式。
红色与action中的属性(有get、set方法)相对应。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<!-- 测试action中的message属性是否有值 -->
<s:if test="message != null">
<font color="red">
<!-- 得到message的值,并显示在网页中 -->
<s:property value="message"/>
</font>
</s:if>
Please enter your name:
<!-- 这是一个 form,将投交的是hello.action网址 -->
<s:form action = "helloxx.action">
<!-- 这是一个文本标签,它与名为Hello的Action的name属性相关联 -->
< s:textfield name ="name"/>
<s:submit/>
</s:form>
</body>
</html>

最后运行
本软件是在netbeans6.5中测试。工程为web2
调试运行:
http://localhost:8084/Web2/helloxx.action

[color=red]注意事项:[/color]
1 action 与strut..xml中的配置要对应。Action类中返回的每种字符串,要在配配置中有节点result相对应 如:Hello.java中返回有"success"字符串,则在struts.xml中,有<result name="success">/hello.jsp</result>相对应

2 struts.xml 的namespace节点配置,意味着其下的返回网页必须与之相对应存放。Result节点的配置也要相对应。如:namespace=”/”,则Result的配置如
<result name="success">/hello.jsp</result>,同时hello.jsp必须存放于网页的根目录中。在netbeans中,hello.jsp必须在web文件夹下。
3 一个action的网址是:
网址根目录+ action名字。
如:Hello.java 的配置为:<action name="[color=blue]helloxx[/color]" class="hi.Hello">
而工程又是web2. 则在netbeans中网址是:
http://localhost:8084/Web2/[color=blue]helloxx[/color].action
[color=blue]注意[/color]:蓝色色字体
4 网址中的action名字要区分大小写
5 网页hello.jsp 与Hello.java的数据交互是通过Hello.java中有get、set的属性进行交互。
6 [b][color=darkblue] struts2中的lib 为[/color][/b]
[quote]commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.3.jar[/quote]
[color=red][b]如果:struts2-spring-plugin 包存在将无法起动 apache tomcat服务器[/b][/color][color=blue]


7struts.xml中可引入别的action配置。便于排错或维护[/color]本例中,可如下用两个xml文件写配置。这两个文件为:Struts.xml , struts-hello.xml。这两个文件的头都一样。下例中两个配置文件同一个文件夹下
Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<include file="struts-hello.xml" />
</struts>

struts-hello.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="hi" namespace="/" extends="struts-default">
<action name="helloxx" class="hi.Hello">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值