当前位置 博文首页 > 程序员springmeng:如何写Action类?

    程序员springmeng:如何写Action类?

    作者:[db:作者] 时间:2021-08-10 16:14

    <span style="font-family: 宋体, Arial; background-color: rgb(255, 255, 255);">1.如何写Action类?</span>

    ? 1)public String execute(){}

    ? 2) public String methodName(){}

    ? 3) extends ActionSupport

    2.Action的方法,怎么样跟请求对应?

    ?? 1)什么都不指定,默认调用Action的execute
    ?? 2)在struts.xml action的method属性指定

    <span style="color: rgb(102, 102, 102);"><?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    	<include file="struts-default.xml"></include>
    	<package name="dd" extends="struts-default" namespace="/">
    	<action name="login" class="com.xiaochuan.LoginAction" </span><span style="color:#ff0000;"><strong>method="login"</strong></span><span style="color:#666666;">>
    	<result name="success">/welcome.jsp</result>
    	<result name="fail">.index.jsp</result>
    	</action>
    	</package>
    </struts>    </span>
    ? ?3)在url中用!号指定??? login!add.action?? (login为action名,add为action中的一个方法名)

    <span style="color:#666666;"><body>
      <form action="</span><span style="font-family: 宋体, Arial; font-size: 16px; line-height: 26px;"><strong><span style="color:#ff0000;">login!add.action?</span></strong></span><span style="color:#666666;">" method="post">
      <input type="text" name="username"><br/>
      <input type="text" name="password"><br/>
      <input type="submit" name="reset"><br/>
      </form>
      </body></span>
    ?4)用submit标签的method属性指定方法名称

    <span style="color:#666666;"><s:form action="login.action" method="post">
      <input type="text" name="username"><br/>
      <input type="text" name="password"><br/>
      <input type="submit" name="reset"><br/>
      <s:sumbit name="login" value="登录" </span><strong><span style="color:#ff0000;">method="login"</span></strong><span style="color:#666666;">></s:sumbit>
      <s:sumbit name="add" value="添加" method="add"></s:sumbit>
      </s:form></span>
    (5) 在action用,用通配符自己适配

    ?<action name="user_*" class="com.xiaochuan.Login.Action" method="{1}">
    ? <result name="success">/welcom.jsp</result>
    ? <result name="fail">/index.jsp</result>
    ? </action>

    3.Action中怎么获取作用域通讯对象
    ?? 1)IOC
    ????? Struts2? (Action对象)
    ????? HttpServletRequest (Tomcat)

    步骤:实现接口SessionAware,重写setSession()方法,在login中调用session.put("key","value");

    public class LoginAction extends ActionSupport implements SessionAware,
            ServletRequestAware {
    
        private String userName;
        private String password;
        private String message;
    
        private Map session = null;
    
        private HttpServletRequest request = null;
     /**
      * Action中如何获取作用域通讯对象 s1)IOC Struts2 (Action对象)
      * 步骤:实现接口SessionAware,重写setSession()方法,在login中调用session.put("key","value");
      * HttpServletRequest (Tomcat)
      * 实现接口ServletRequestAware接口,重写setServletRequest()方法 2)非IOC
      * 
      */
    
        public String login() {
            String result = "success";
            message = "登录成功";
            System.out.println(userName + " " + password);
            if (userName == null || userName.equals("")) {
                result = "fail";
                message = "登录失败";
            } else {
                // 获取session
                // 在session中放一个用户登陆成功标记
                session.put("LOGIN_FLAG", userName);
                // 非IOC
                Map s1 = ActionContext.getContext().getSession();
                HttpServletRequest req = ServletActionContext.getRequest();
            }
            return result;
        }
    
    // 重写了SessionAware接口中的setSession方法
      
        public void setSession(Map<String, Object> arg0) {
            this.session = arg0;
        }

    ?struts2将session用map为了:实现容器的解耦(测试时只需要在JUnit测试,只需要在Map中看有无值,否则需要在tomcat下测试)
    ?? 2)非IOC

    ????? HttpServletRequest req?=?ServletActionContext.getRequest();




    cs