zoukankan      html  css  js  c++  java
  • Struts2笔记2—基础知识1

    1、struts.xml中的package有个属性namespace,如下:

    <package name="default" namespace="/" extends="struts-default">
            <action name="hello">
                <result>
                   /Hello.jsp
                </result>
            </action>
        </package>
        
         <package name="front" namespace="/front" extends="struts-default">
            <action name="index">
                <result>
                   /Index.jsp
                </result>
            </action>
        </package>

    上述两个aciotn的url分别为:

    http://localhost:8080/StrutsDemo/hello

    http://localhost:8080/StrutsDemo/front/index

    由此可以看出,namespace是项目名称以及action之间的一段,用于对action进行分模块的。

    注意:若namespace="",即等于空,则url这一段可以随意定义,只要出现action名称即会被响应。

    2、关于Action,在Struts.xml中可以用class来定义响应的类:

    <package name="front" extends="struts-default" namespace="/">
            <action name="index" class="com.zsp.action.IndexAction">
                <result name="success">/Index.jsp</result>
               <result name="error">/Error.jsp</result>
            </action>
        </package>

    其中IndexAction有3种实现方法:

    <1>直接定义Java类,不需要继承接口和类,编写execute方法即可(不推荐)

    public class IndexAction {
        public String execute() {
            return "success";
        }
    }

    <2>继承Action接口,重写execute()方法(不推荐)

    import com.opensymphony.xwork2.Action;
    
    public class IndexAction implements Action {
        @Override
        public String execute() {
            return "success";
        }
    }

    <3>继承ActionSupport类,重写execute()方法:

    import com.opensymphony.xwork2.ActionSupport;
    
    public class IndexAction extends ActionSupport {
        @Override
        public String execute() {
            return SUCCESS;
        }
    }

    ActionSupport继承Action接口,Action中定义了一些常量作为返回值:

    public static final String SUCCESS = "success";
    public static final String NONE = "none";
    public static final String ERROR = "error";
    public static final String INPUT = "input";
    public static final String LOGIN = "login";

    Struts会根据不同的返回值指向不同的jsp页面,也可以自定义返回值。

    3、路径问题,在开发中应该使用绝对路径,避免使用相对路径,可以在JSP页面如下定义:

    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    %>
    
    <head>
    <base href="<%=basePath%>">
    </head>

    这样页面内的url就会自动在拼接到basePath之后。

    4、Struts的动态调用(DMI)

    在Struts.xml中,可以定义该Action执行的是哪个方法,而不一定非得是execute(),如下:

    <action name="userAdd" class="com.zsp.action.UserAction" method="add">
                <result>/user_add_success.jsp</result>
            </action>

    但如果类似的比较多,那么xml中就要定义很多的Action,所以这不是比较好的方法,可以在url中如此输入:

    http://localhost:8080/StrutsDemo/user!add

    使用感叹号,意味着该Action要执行add()方法,此时Struts.xml配置如下:

    <action name="user" class="com.zsp.action.UserAction">
                <result>/user_add_success.jsp</result>
            </action>
  • 相关阅读:
    基于NFS共享存储实现KVM虚拟主机动态迁移
    基于mysqld_multi实现MySQL 5.7.24多实例多进程配置
    LVS负载均衡实现双向设备
    基于Haproxy构建负载均衡集群
    基于Haproxy+Keepalived构建高可用负载均衡集群
    nginx与keepalived实现高可用
    直接路由模式(LVS-DR)
    Tomcat多实例配置
    Tomcat 安全优化
    基于 Jenkins + Git 项目 中Git主机的 安装配置
  • 原文地址:https://www.cnblogs.com/zsp0817/p/3523339.html
Copyright © 2011-2022 走看看