*五)与ServletAPI解耦

方式1

AddAction

public String execute() throws Exception, IOException{
//获取请求对象request 响应对象response 应用对象ServletContext
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
ServletContext context=ServletActionContext.getServletContext();
//获取表单参数
Integer num1=Integer.parseInt(request.getParameter("num1"));
Integer num2=Integer.parseInt(request.getParameter("num2"));
Integer sum=num1+num2;
//将请求结果放在应用对象中
context.setAttribute("sum", sum);
//将结果放在域对象request中
//request.setAttribute("sum", sum);
//转发到add.jsp中 由struts.xml中的result代替
//request.getRequestDispatcher("/add.jsp").forward(request, response);
return "success";
}

struts_add.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="add" extends="struts-default" namespace="/">
<action name="add" class="cn.itcast.web.struts2.add.AddAction">
<result name="success" type="redirect">
/add.jsp
</result>
</action>
</package>
</struts>

add.jsp

${applicationScope.sum}

方式2

AddAction

public String execute() throws Exception, IOException{
//获取请求对象request 响应对象response 应用对象ServletContext
HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
//方式2
Map<String,Object> applicationMap=ActionContext.getContext().getApplication(); //获取表单参数
Integer num1=Integer.parseInt(request.getParameter("num1"));
Integer num2=Integer.parseInt(request.getParameter("num2"));
Integer sum=num1+num2; applicationMap.put("sum", sum);
//将结果放在域对象request中
//request.setAttribute("sum", sum);
//转发到add.jsp中 由struts.xml中的result代替
//request.getRequestDispatcher("/add.jsp").forward(request, response);
return "success";
}

add.jsp仍然是${applicationScope.sum}

原则:Action不要与传统ServletAPI耦合  目的:解耦

方式3AddAction

public String execute() throws Exception, IOException{
//获取请求对象request 响应对象response 应用对象ServletContext
//HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
//方式3
HttpSession session=request.getSession();//用了Servlet的API,不推荐
Map<String,Object> sessionMap=ActionContext.getContext().getSession();
//获取表单参数
Integer num1=Integer.parseInt(request.getParameter("num1"));
Integer num2=Integer.parseInt(request.getParameter("num2"));
Integer sum=num1+num2;
sessionMap.put("sum", sum);
//将结果放在域对象request中
//request.setAttribute("sum", sum);
//转发到add.jsp中 由struts.xml中的result代替
//request.getRequestDispatcher("/add.jsp").forward(request, response);
return "success";
}

add.jsp  ${sessionScope.sum}

(1)获取HttpServletRequest请求对象/域对象【ServletActionContext.getRequest()】

    的父类【ActionContext.getContext().put("username",username);】

  ActionContext.getContext()返回Map<String,Object>
  获取HttpServletResponse请求对象/域对象【ServletActionContext.getResponse()】
(2)获取ServletContext域对象【ActionContext.getContext().getApplication()】
(3)获取HttpSession域对象【ActionContext.getContext().getSession()】
(4)为什么(2)(3)会返回Map类型呢?
  这是因为Struts2的Action中,不推荐引用ServletAPI,因为只返回Map对象。
  拦截器会将Map中的值,再一次绑定到真实的域对象中,其中key就是Map中的key值。

(5)

day31\WebRoot\register.jsp

 <form action="/day31/register" method="post">
<table border="2" align="center">
<caption><h3>新用户注册(struts2版本)</h3></caption>
<tr>
<th>用户名</th>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<th>密码</th>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="注册"/>
</td>
</tr>
</table>
</form>
cn.itcast.web.struts2.user.UserAction
package cn.itcast.web.struts2.user;

public class UserAction {
//表单参数
private String username;
private String password;
//提供对应的setter方法(拦截器会将表单参数通过setter方法自动注入进来)
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
//注册的业务控制方法
public String registerMethod(){
System.out.println("用户名:" + username);
System.out.println("密码:" + password);
return "success";
}
}

/day31/src/cn/itcast/web/struts2/user/struts_user.xml(在总的文件/day31/src/struts.xml中包含)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <!-- struts2的核心配置文件,在应用部署时加载并解析 -->
<struts>
<include file="cn/itcast/web/struts2/user/struts_user.xml"/>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<package name="user" extends="struts-default" namespace="/">
<!-- action name对应form中的action -->
<action
name="register"
class="cn.itcast.web.struts2.user.UserAction"
method="registerMethod">
<result name="success" type="dispatcher">
/WEB-INF/register_success.jsp
</result>
</action>
</package>
</struts>

/day31/WebRoot/WEB-INF/register_success.jsp

  <body>
注册成功<br/>
</body>

对于POST请求,框架默认采用UTF-8编码方式,无需设置/day31/src/struts.properties
struts.i18n.encoding=UTF-8  类似于:request.setCharacterEncoding("UTF-8")

以上配置,只针对POST请求。
struts2框架提倡按如下原则发送请求:
A)无参使用GET,框架没提交解决方案
B)有参使用POST
(6)项目中使用POST和GET请求方式的原则
【struts2以POST或GET方式传中文参数】

(4)获取servlet常用api的更多相关文章

  1. Servlet 常用API学习(三)

    Servlet常用API学习 (三) 一.HTTPServletRequest简介 Servlet API 中定义的 ServletRequest 接口类用于封装请求消息. HttpServletRe ...

  2. Servlet 常用API学习(二)

    Servlet常用API学习 一.HTTP简介 WEB浏览器与WEB服务器之间的一问一答的交互过程必须遵循一定的规则,这个规则就是HTTP协议. HTTP是 hypertext transfer pr ...

  3. Servlet 常用API学习(一)

    Servlet常用API学习 一.Servlet体系结构(图片来自百度图片) 二.ServletConfig接口 Servlet在有些情况下可能需要访问Servlet容器或借助Servlet容器访问外 ...

  4. Struts2获取Servlet的api的两种方式,解决ParameterAware过时的问题

    servlet API通过ActionContext进行获取 Struts2对HttpServletRequest,HttpSession和ServletContext进行了封装,构造了3个Map对象 ...

  5. 获取Servlet原生API

    1.请求 <a href="param/test1">Servlet原生API</a> 2.处理方法 @RequestMapping("/para ...

  6. struts2中在Action中如何获取servlet的api?

    1.通过ActionContext类(拿到的不是真正的servlet api,而是一个map) ActionContext context = ActionContext.getContext(); ...

  7. Struts2框架中使用Servlet的API示例

    1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的 ...

  8. 在Struts2框架中使用Servlet的API

    1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Action中使用Servlet的API接收到,然后保存到三个域对象中,最后再显示到JSP的 ...

  9. Struts2中获取servlet API的几种方式

    struts2是一个全新的MVC框架,如今被广大的企业和开发者所使用,它的功能非常强大.这给我们在使用servlet 纯java代码写项目的时候带来了福音.但是一般来说,我们的项目不到一定规模并不需要 ...

随机推荐

  1. Linux下防火墙iptables用法规则详及其防火墙配置

    转:http://www.linuxidc.com/Linux/2012-08/67952.htm iptables规则 规则--顾名思义就是规矩和原则,和现实生活中的事情是一样的,国有国法,家有家规 ...

  2. Java内存区域与模拟内存区域异常

    我把Java的内存区域画了一张思维导图,以及各区域的主要功能. 模拟Java堆溢出 Java堆用于存储对象实例.仅仅要不断地创建对象而且保证GC ROOTS到对象之间有可达路径避免被回收机制清除.就能 ...

  3. Linux内核——内存管理

    内存管理 页 内核把物理页作为内存管理的基本单位.内存管理单元(MMU,管理内存并把虚拟地址转换为物理地址)通常以页为单位进行处理.MMU以页大小为单位来管理系统中的页表. 从虚拟内存的角度看,页就是 ...

  4. Openstack nova代码部分凝视一

    做个一个没怎么学过python的菜鸟.看源代码是最好的学习方式了,如今就从nova入手,主要凝视一下 nova/compute/api.py 中的 create_instance函数 def _cre ...

  5. Andfix热修复框架原理及源代码解析-上篇

    热补丁介绍及Andfix的使用 Andfix热修复框架原理及源代码解析-上篇 Andfix热修复框架原理及源代码解析-下篇 1.不知道怎样使用的同学,建议看看我上一篇写的介绍热补丁和Andfix的使用 ...

  6. Odoo11 新功能 : 栏位隐藏

        Odoo11增加了一个 新的 modifier-. 先上 前端框架代码     invisible / readonly / required 这几个 是经常在用的, column_invis ...

  7. WPF非UI线程中调用App.Current.MainWindow.Dispatcher提示其他线程拥有此对象,无权使用。

    大家都知道在WPF中对非UI线程中要处理对UI有关的对象进行操作,一般需要使用委托的方式,代码基本就是下面的写法 App.Current.MainWindow.Dispatcher.Invoke(ne ...

  8. VueJS事件处理器v-on:事件修饰符&按键修饰符

    事件修饰符 Vue.js 为 v-on 提供了事件修饰符来处理 DOM 事件细节,如:event.preventDefault() 或 event.stopPropagation(). Vue.js通 ...

  9. npm常用命令(转)

    npm install <name>安装nodejs的依赖包 例如npm install express 就会默认安装express的最新版本,也可以通过在后面加版本号的方式安装指定版本, ...

  10. Oracle SQL性能优化 - 根据大表关联更新小表

    需求: 小表数据量20w条左右,大表数据量在4kw条左右,需要根据大表筛选出150w条左右的数据并关联更新小表中5k左右的数据. 性能问题: 对筛选条件中涉及的字段加index后,如下常规的updat ...