struts入门学习(二)

一  struts的各种视图的转发与重定向

1 struts跳转到指定的JSP页面,只需要修改配置文件

<package name="user" namespace="/user" extends="struts-default">

<action name="login">
                     <result>/WEB-INF/page/login.jsp</result>
              </action>
        </package>

        访问http://localhost/Struts/user/login.do即可跳转到login.jsp页面

  

      2  带条件的跳转

           <action name="verify" class="com.day02.demo1" method="execute">

<result name="login">/WEB-INF/page/login.jsp</result>
                      <result name="register">/WEB-INF/page/register.jsp</result>
                      <result name="default">/WEB-INF/page/default.jsp</result>
          </action>

          当你访问http://localhost/Struts/user/verify.do?message=login 跳转到login.jsp页面

          当你访问http://localhost/Struts/user/verify.do?message=register 跳转到register.jsp页面

          当你的url中的message的值不是login和register时就凸凹转到default页面

      3 动态条用方法值

          (1) 只需要在url后面加上 !方法名即可

                <action name="Method" class="com.day02.demo2">

<result>/WEB-INF/page/trendsMethod.jsp</result>
                </action>

在demo2中有三个方法一个属性

private String message;

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

public String login(){
                            this.message = "loginVerify()";
                            return "success";
                 }

public String register(){
                           this.message = "registerVerify()";
                           return "success";
               }

public String execute(){
                           this.message = "execute()";
                          return "success";
               }

              http://localhost/Struts/user/Method!login.do 即可指定调用的方法是login()

             http://localhost/Struts/user/Method!register.do 即可指定调用的方法是login()

             http://localhost/Struts/user/Method则执行默认的方法名execute()

            可以使用struts中的配置项来指定是否禁用方法的动态调用

             <struts>

                         <constant name="struts.devMode" value="true" />    <!--设置为开发模式修改后只需保存不用重新发布项目-->
                         <constant name="struts.action.extension" value="do" />   <!--后缀名设置为do-->
                         <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!--开启动态方法调用 为false时为禁用动态方法调用-->

</struts>

4 使用通配符来动态调用方法

<action name="Method_*" class="com.day02.demo2" method="{1}">

                     <result>/WEB-INF/page/trendsMethod.jsp</result>
           </action>

            只需要访问http://localhost/Struts/user/Method_login.do 即可调用login()方法

             同上

        5 重定向

            struts中的重定向是在result中添加参数来指定的

            <action name="login2">

                        <result type="redirect">/login.jsp</result>
            </action>

            访问http://localhost/Struts/user/login2.do 就自动跳转到http://localhost/Struts/login.jsp

             注意 : 跳转的页面不能再WEB-INF下面

            跳转传递参数问题请参考博文 http://www.cnblogs.com/dbqjava/p/4350756.html

       6   action之间的跳转

            (1) 在同一个包下的跳转

                     <action name="login">

                                 <result>/WEB-INF/page/login.jsp</result>
                     </action>

<action name="login2">
                                 <result type="redirectAction">login</result>
                    </action>

                    访问http://localhost/Struts/user/login2.do ----->重定向到http://localhost/Struts/userlogin.do------>转发到/WEB-INF/page/login.jsp

             (2) 要重定向的action不在同一个package中

                   <action name="login2">

                           <result type="redirectAction">
                                    <param name="actionName">login</param><!--指定要访问的转发的action名称-->
                                    <param name="namespace">/test</param><!--指定要转发的package的namespace名称-->
                           </result>
                   </action>

                   要重定向到另外的一个package下的action

<package name="other" namespace="/test" extends="struts-default">
                           <action name="login"> 
                                 <result>/WEB-INF/page/login.jsp</result> 
                           </action>
                    </package>

上一篇:CVE-2016-2502-drivers/usb/gadget/f_serial.c in the Qualcomm USB driver in Android. Buffer Overflow Vulnerability reported by #plzdonthackme, Soctt.


下一篇:js 数组的常用方法