Struts2笔记 - struts.xml文件的配置

发布于 2011-06-19 | 更新于 2020-09-20

1、模块包含:

可以使用include标签在struts的配置文件中包含其他的配置文件:

2、设置默认的Action:

通过default-action-ref标签设置默认的action:



/default.jsp

3、配置带结果类型的result标签:

通过result标签的type属性进行设置:

/r1.jsp /r2.jsp r1 r2

4、配置全局结果集:

通过global-results标签设置全局结果集:

/login.jsp ...
<package name="student" namespace="/student" extends="user">
    <action name="student" class="com.action.UserAction">
        <result>/exam.jsp</result>
    </action>
</package>

5、配置动态结果集:

动态结果集就是根据执行结果中传过来的参数通过OGNL表达式${}从值栈中取值作为action的result:

Action:

public String execute() throws Exception {
if(type == 1) r=“/success.jsp”;
else if (type == 2) r=“/login.jsp”;
return “success”;
}

struts.xml:

${r}

6、配置带参数的结果集:

即是在result标签的结果URL中通过OGNL表达式的方式给这个URL添加参数信息:

Action:

private int type;
@Override
public String execute() throws Exception {
return “success”;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}

struts.xml:

/user_success.jsp?t=${type}

这个type可以是Action的执行函数赋值的,也可以是从表单或者URL中传过来的。 JSP: 在URL中指定type的值:

传参数

本文作者: arthinking

本文链接: https://www.itzhai.comthe-struts2-notes-struts-xml-files-configuration.html

版权声明: 版权归作者所有,未经许可不得转载,侵权必究!联系作者请加公众号。

×
IT宅

关注公众号及时获取网站内容更新。