Struts2 (中)

本文详细介绍了在Struts2框架中如何集成Servlet API,包括通过ActionContext获取API、使用servletActionContext操作域对象及接口注入方式。并提供了具体实例代码,展示了如何在Struts2中使用Servlet的API进行参数获取、数据存储等操作。

Struts中API介绍

ServletAPI

在使用Struts2的框架的过程中,发现Struts2和Servlet的API是解耦合的。
在实际开发中,经常使用到Servlet的API,比如进行登录,将用户的信息保存到Session中,有的时候需要向页面输出一些内容,用到response对象。涉及到Servlet的API的访问。

如何获得到servlet中的API

完全解耦合的方式
  • 使用ActionContext来进行获取
  • 注意
    • 此方法只能从域中取数据和保存数据
    • 不能获取其它的方法
使用步骤
<form action="${pageContext.request.contextPath }/myform.action"
    method="get">
    用户名:<input type="text" placeholder="请输入用户名..." name="username"><br />
    昵称:<input type="text" placeholder="请输入用户名..." name="nick"><br />
    爱 好: 
    <input type="checkbox" value="足球" name="hobby">足球 
    <input type="checkbox" value="篮球" name="hobby">篮球 
    <input type="checkbox" value="乒乓球" name="hobby">乒乓球<br /> 
    <input type="submit" value="提交">
</form>
<struts>
    <package name="struts" extends="struts-default" namespace="/">
        <action name="myform" class="com.xzh.struts2.MyFormAction">
            <!-- 结果页 -->
            <result name="success">/myxq.jsp</result>
        </action>
    </package>
</struts>
public class MyFormAction extends ActionSupport {
    public String execute() {
        // 获取参数
        ActionContext context = ActionContext.getContext();
        HttpParameters parameters = context.getParameters();
        // 根据key取值
        String username = parameters.get("username").getValue();
        System.out.println(username);
        String nick = parameters.get("nick").getValue();
        System.out.println(nick);
        String[] hobbys = parameters.get("hobby").getMultipleValues();
        System.out.println(Arrays.toString(hobbys));
        
        // 获取所有参数
        Set<Map.Entry<String, Parameter>> MapEntry = parameters.entrySet();
        for (Map.Entry<String, Parameter> entry : MapEntry) {
            System.out.println(entry.getKey());
            System.out.println(Arrays.toString(entry.getValue().getMultipleValues()));
        }
        
        // 把数据存放到域中
        context.put("resName", "resValue");
        context.getSession().put("sessionName", "sessionValue");
        context.getApplication().put("ApplicationName", "ApplicationValue");
        
        return SUCCESS;
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>myxq</h1>

res:${reqName }
session:${sessionName }
Application:${ApplicationName }

</body>
</html>
示例表单
<form>
    用户名:<input type="text" placeholder="请输入用户名..." name="username"><br/>
    昵称:<input type="text" placeholder="请输入用户名..." name="nick"><br/>
    爱  好: <input type="checkbox" value="足球" name="hobby">足球
            <input type="checkbox" value="篮球" name="hobby">篮球
            <input type="checkbox" value="乒乓球" name="hobby">乒乓球<br/>
    <input type="submit" value="提交">
</form>
使用Servlet的API的原生方式
  • servletActionContext
  • 这种方式可以操作域对象的数据,同时也可以获得对象的方法。
  • 使用步骤
<form action="${pageContext.request.contextPath }/myform.action"
    method="get">
    用户名:<input type="text" placeholder="请输入用户名..." name="username"><br />
    昵称:<input type="text" placeholder="请输入用户名..." name="nick"><br />
    爱 好: 
    <input type="checkbox" value="足球" name="hobby">足球 
    <input type="checkbox" value="篮球" name="hobby">篮球 
    <input type="checkbox" value="乒乓球" name="hobby">乒乓球<br /> 
    <input type="submit" value="提交">
</form>
<struts>
    <package name="struts" extends="struts-default" namespace="/">
        <action name="myform" class="com.xzh.struts2.MyFormAction">
            <!-- 结果页 -->
            <result name="success">/myxq.jsp</result>
        </action>
    </package>
</struts>
public class MyFormAction extends ActionSupport {
    public String execute() {
        // 获取原生api
        HttpServletRequest request = ServletActionContext.getRequest();

        // 根据key取值
        String username = request.getParameter("username");
        System.out.println(username);
        String nick = request.getParameter("nick");
        System.out.println(nick);
        String[] hobbys = request.getParameterValues("hobby");
        System.out.println(Arrays.toString(hobbys));

        // 获取所有参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        for (String key : parameterMap.keySet()) {
            String[] values = parameterMap.get(key);
            System.out.println(key + " " + Arrays.toString(values));
        }

        // 把数据存放到域中
        request.setAttribute("resName", "resValue");
        request.getSession().setAttribute("sessionName", "sessionValue");
        ServletActionContext.getServletContext().setAttribute("ApplicationName", "ApplicationValue");

        return SUCCESS;
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<h1>myxq</h1>

res:${reqName }
session:${sessionName }
Application:${ApplicationName }

</body>
</html>
接口注入的方式

让Action实现一些接口,让接口提供的一些方法,设置一些具体的值

使用步骤
1472533-20190426235540285-180630408.png

结果页配置

全局配置

  • 全局结果页面:全局结果页面指的是,在包中配置一次,
  • 其他的在这个包中的所有的action只要返回了这个值,都可以跳转到这个页面。
使用场景

有些功能需要用户登入才能使用,只要没有登录,就让它返回到登录页面,很多地方都要返回到登录,所以可以把登录页做为一个全局的

使用方法

1472533-20190426235545530-1362673787.png

局部配置

局部结果页面:局部结果页面指的是,只能在当前的action中的配置有效

使用方法

1472533-20190426235551414-590209299.png

注意点:全局和局部名称相同时,会优先找局部

转载于:https://www.cnblogs.com/xzh0717/p/10753463.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值