Struts2 学习笔记6--访问web元素-模块包含

访问web元素

struts.xml:
<package name="login" extends="struts-default" namespace="/login">
        <action name="login*" class="com.bjsxt.struts2.user.action.LoginAction{1}">
            <result>/user_login_success.jsp</result>
        </action>
    </package>


jsp:
<form name="f" action="" method="post">
用户名:<input type="text" name="name"/>
密码:<input type="text" name="password"/>
<br />
<input type="button" value="submit1" onclick="javascript:document.f.action='login/login1';document.f.submit();" />
<input type="button" value="submit2" onclick="javascript:document.f.action='login/login2';document.f.submit();" />
<input type="button" value="submit3" onclick="javascript:document.f.action='login/login3';document.f.submit();" />
<input type="button" value="submit4" onclick="javascript:document.f.action='login/login4';document.f.submit();" />
</form>


result jsp:
    User Login Success!
    <br />
    <s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />
    <s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />
    <s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />
    <s:property value="#attr.a1"/><br />
    <s:property value="#attr.s1"/><br />
    <s:property value="#attr.r1"/><br />
    <s:debug></s:debug>
    <br />

注意:#attr表示模糊查找属性值,在request>session>application三个范围中查找属性。

 

 

访问request等三种方式:
1、从上下文中取得,直接赋值
    public LoginAction1() {
        request = (Map)ActionContext .getContext().get("request");
        session = ActionContext.getContext().getSession();
        application = ActionContext.getContext().getApplication();
    }


2、 实现RequestAware,SessionAware, ApplicationAware三个接口,依赖注入(控制反转)   最常用
public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {
    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;
   
    //DI dependency injection
    //IoC inverse of control
    public String execute() {
        request.put("r1", "r1");
        session.put("s1", "s1");
        application.put("a1", "a1");
        return SUCCESS;
    }

    @Override
    public void setRequest(Map<String, Object> request) {
        this.request = request;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    @Override
    public void setApplication(Map<String, Object> application) {
        this.application = application;
    }
}


3、直接从ServletActionContext中获取
    public LoginAction3() {
        request = ServletActionContext .getRequest();
        session = request.getSession();
        application = session.getServletContext();
    }


4、实现ServletRequestAware接口,实现setServletRequest方法
public class LoginAction4 extends ActionSupport implements ServletRequestAware {
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;
   
    public String execute() {
        request.setAttribute("r1", "r1");
        session.setAttribute("s1", "s1");
        application.setAttribute("a1", "a1");
        return SUCCESS;
    }
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
        this.session = request.getSession();
        this.application = session.getServletContext();
    }
   
}


结论:
取得Map类型request,session,application,真实类型 HttpServletRequest, HttpSession, ServletContext的引用:
1.前三者:依赖于容器
2.前三者:IOC (只用这种)
3.后三者:依赖于容器
4.后三者:IOC

 

 

模块包含
在struts.xml中配置可包含其他xml中的配置,这样利于模块配置之间解耦
struts.xml:
<struts>
    <constant name="struts.devMode" value="true" />
    <include file="login.xml" />
</struts>

login.xml
<struts>
    <package name="login" extends="struts-default" namespace="/login">
        <action name="login*" class="com.bjsxt.struts2.user.action.LoginAction{1}">
            <result>/user_login_success.jsp</result>
        </action>
    </package>
</struts>


默认Action(默认当找不到Action,就由这个Action处理)
注意:default-action-ref不能转发Action,只能转发到结果JSP页面,不会去调用Action#execute
所以bug,一般不要用,使用其他方式,如web.xml:
<welcome-file-list>
    <welcome-file>index</welcome-file>
</welcome-file-list>

配置:
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index"></default-action-ref>
        <action name="index">
            <result>/default.jsp</result>
        </action>
</package>


在struts2文档中这样说明:
Action Default
Usually, if an action is requested, and the framework can't map the request to an action name, the result will be the usual "404 - Page not found" error. But, if you would prefer that an omnibus action handle any unmatched requests, you can specify a default action. If no other action matches, the default action is used instead.
<package name="Hello" extends="action-default">

    <default-action-ref name="UnderConstruction"/>

    <action name="UnderConstruction">
        <result>/UnderConstruction.jsp</result>
    </action>

    ...
There are no special requirements for the default action. Each package can have its own default action, but there should only be one default action per namespace.
     One to a Namespace
The default action features should be set up so that there is only one default action per namespace. If you have multiple packages declaring a default action with the same namespace, there is no guarantee which action will be the default.


说明:每个namespace中只能配置一个default-action-ref。

Action总结:
1.    实现一个Action的最常用方式:从ActionSupport继承
2.    DMI动态方法调用 !
3.    通配符配置 * {1} {2} …
a)    *_*
4.    接收参数的方法(一般用属性或者DomainModel来接收)
5.    简单参数验证addFieldError
a)    一般不使用Struts2的UI标签
6.    访问Web元素
a)    Map类型
i.    IoC
ii.    依赖Struts2
b)    原始类型
i.    IoC
ii.    依赖Struts2
7.    包含文件配置
8.    默认action处理

 

 

 

 

 

你可能感兴趣的:(JavaScript,Web,jsp,struts,IOC)