Struts2 核心组件之 struts.xml

Struts2 核心组件之 struts.xml  

2011-01-05 23:11:28|  分类: Struts |  标签:struts.xml   |字号 订阅

 

<constant name="strtus.action.extension" value="do"/> 元素:该元素的“name”属性值是struts.properties 中的键名,“value”属性值是键的值。在这里设置后将覆盖struts.properties 中该键的值。这样做的好处是,当使用了多个struts.xml 映射文件,可以为每个文件设置不同的属性。

<include file=" example.xml" />元素: 导入一个子文件,子文件与struts.xml 有相同的结构,使用相同的配置语法。通过这种方式提供了一种模块化来管理struts.xml文件。

<package name="main" extends="struts-default">
     <action name="index">
           <result>login.jsp</result>
      </action>
</package>

<package>元素的“name” 表示包的名字,“extends” 表示继承自struts-default.xml 中定义的包的名字。struts-default.xml 中定义了一个名为“struts-default” 的包,这样“struts-default”包中的拦截器都可以在包“main”中使用。

 <action>元素,按照作用的不同,可以配置5中类型不同的请求映射。

㈠ 配置直接转发的请求
<action name="index">
           <result>login.jsp</result>
</action>

例:index.jsp

      <a href="index.do">到登陆页面</a>

㈡ 指定处理的Action类

<action name="login" class="com,demo.struts2.actions.LoginAction"">
           <result name="success">welcome.jsp</result>
           <result name="login">login.jsp</result>
 </action>
例:login,jsp

<form name="form1" action="login.do" method="post">
            Username:<input type="text" name="username" maxlength="20"><br>
            Passwrod:<input type="password" name="password" maxlength="20"><br>
            <input type="submit" value="登陆">
</form>

LoginAction.java

 private String username;
 private String password;

public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }

@SuppressWarnings("unchecked")
 public String execute() throws Exception{
              if(username.equals("joe")&&password.equals("123")){
                      ActionContext.getContext().getSession().put("username", username);
                      ActionContext.getContext().getSession().put("password", password);
                      return "success";
              }else{
                      return "login";
             }
 }

welcome.jsp

<h1>欢迎<%=(String)session.getAttribute("username")%></h1><br>

 ㈢ 指定Action的处理方法

<action name="login" class="com,demo.struts2.actions.LoginAction" method="login">
           <result name="success">welcome.jsp</result>
           <result name="login">login.jsp</result>
 </action>

例:login,jsp

<form name="form1" action="login.do" method="post">
            Username:<input type="text" name="username" maxlength="20"><br>
            Passwrod:<input type="password" name="password" maxlength="20"><br>
            <input type="submit" value="登陆">
</form>

LoginAction.java

@SuppressWarnings("unchecked")
 public String login() throws Exception{
              if(username.equals("joe")&&password.equals("123")){
                      ActionContext.getContext().getSession().put("username", username);
                      ActionContext.getContext().getSession().put("password", password);
                      return "success";
              }else{
                      return "login";
             }
 }

welcome.jsp

<h1>欢迎<%=(String)session.getAttribute("username")%></h1><br>

㈣ 通过请求地址来指定执行的函数

<action name="login" class="com,demo.struts2.actions.LoginAction">
           <result name="success">welcome.jsp</result>
           <result name="login">login.jsp</result>
 </action>

例:login,jsp

<form name="form1" action="login!login.do" method="post">
            Username:<input type="text" name="username" maxlength="20"><br>
            Passwrod:<input type="password" name="password" maxlength="20"><br>
            <input type="submit" value="登陆">
</form>

 LoginAction.java

@SuppressWarnings("unchecked")
 public String login() throws Exception{
              if(username.equals("joe")&&password.equals("123")){
                      ActionContext.getContext().getSession().put("username", username);
                      ActionContext.getContext().getSession().put("password", password);
                      return "success";
              }else{
                      return "login";
             }
 }

public String logout() throws Exception{
           HttpSession session = ServletActionContext.getRequest().getSession(false);
           if(session != null)
                    session.invalidate();
           return "login";
 }

welcome.jsp

<h1>欢迎<%=(String)session.getAttribute("username")%></h1><br>

<a href="login!logout.do">注销</a>

㈤ 配置通配符映射

要使用通配符映射,必须设置struts.properties 中的 struts.enable.DynamicMethodInvocation = true;或在struts.xml中添加<constant name="struts.enable.DynamicMethodInvocation " value="true"/>

<action name="login*" class="com,demo.struts2.actions.LoginAction">
           <result name="success">welcome.jsp</result>
           <result name="input">{1}.jsp</result>
 </action>

例:login,jsp

<form name="form1" action="loginlogin!login.do" method="post">
            Username:<input type="text" name="username" maxlength="20"><br>
            Passwrod:<input type="password" name="password" maxlength="20"><br>
            <input type="submit" value="登陆">
</form>

 LoginAction.java

@SuppressWarnings("unchecked")
 public String login() throws Exception{
              if(username.equals("joe")&&password.equals("123")){
                      ActionContext.getContext().getSession().put("username", username);
                      ActionContext.getContext().getSession().put("password", password);
                      return "success";
              }else{
                      return "input";
             }
 }

public String logout() throws Exception{
           HttpSession session = ServletActionContext.getRequest().getSession(false);
           if(session != null)
                    session.invalidate();
           return "input";
 }

welcome.jsp

<h1>欢迎<%=(String)session.getAttribute("username")%></h1><br>

<a href="loginerror!logout.do">注销</a>

error.jsp

已注销!

注:可以在“name”中添加多个“*”,分别用{1}、{2}、{3}…来取代多个匹配的字符串了。

<result>元素:负责完成两项工作,一是提供一个逻辑名,用于于Action 类的返回字符串进行匹配;二是提供一个返回类型(Result Type)。

我们可以为每个包设置默认的返回类型,如果一个包继承了另一个包,它可以选择设置自己的默认返回类型或者直接使用父包的类型。

struts-default.xml中设置默认返回类型的方式如下:

<result-types>
      <result-type name="dispatcher" default="true" class="org.apache.struts2.dispatcher.ServletDispatcherResult"/>
</result-types>

有时候我们的Result 在运行前可能是未知的。比如,一个Result 它跳转的页面取决于它所在Action类的运行结果或者客户端的输入等,这时候我们可以使用动态的Result,即Result的值可以用表达式语言 (EL)来表示,这个表达式是动态的,取决于Action的运行情况。

例如:

<action name="fragment" class="FragmentAction">
           <result name="next" type="redirect-action">${nextAction}</result>
</action>

注:redirect-action 是struts-default.xml 文件中的struts-default包中的其中一个返回类型名。

FragmentAction.java

private String nextAction;

public String getNextAction(){
          return nextAction;
}

<global-results>  配置全局映射元素:这些全局的<result>会被多个<action>所共享。框架会首先寻找嵌在<action>元素中的<result>,如果没有匹配的就会去全局<result>中寻找。

<global-results>
        <result name="error">/error.jsp</result>
        <result name="invalid.token">/error.jsp</result>
        <result name="login" type="redirect-action">login!input</result>
</global-results>

<interceptors> 配置拦截器

我们可以在Action中的方法执行之前先执行一些我们事先定义好的方法,也可以在Action中的方法执行之后立即执行一些我们事先定义好的方法。在开发的过程中拦截器是一个强力的工具。有很多功能:

● 校验(Invalidation):检查输入是否正确

● 属性封装(property population):将输入传输和转化为对象的属性。

● 日志(logging):记录关于每个Action的详细信息。

● 切面(profiling):记录Action的吞吐量。

我们可以将多个拦截器链接在一起形成一个拦截栈(interceptor stack)。比如,一个Action不仅要对客户端的资源进行审查,还要记录它自己的行为,那么我们可以将这两个功能的拦截器放在一起,形成一个拦截 栈。拦截器是以java类的形式实现的,因此每个拦截器都有一个唯一的类名。

例:<interceptors>
                   <interceptor name="security" class="com.company.security.SecurityInterceptor"/>
                   <interceptor-stack name="secureStack">
                             <interceptor-ref name="security"/>
                              <interceptor-ref name="defaultStack"/>
                   </interceptor-stack>
      </interceptors>

拦截器和拦截栈可以任意的顺序混合在一起,Struts2 框架将会按照拦截器在拦截栈的顺序调用它们。大多数应用程序都会定义一个默认的拦截栈,默认的拦截栈会作用于<package>中的每 个<action>上。当然<action>还可以定义它自己的本地(局部)栈。

例:<action name="VelocityCounter" class="org.apache.struts2.example.counter.SimpleCounter">
                   <result name="success">success.jsp</result>
                   <interceptor-ref name="defaultComponentStack"/>
       </action>

posted @ 2013-06-05 22:04  天涯海角路  阅读(185)  评论(0)    收藏  举报