首页 > Windows开发 > 详细

struts2开发2--struts2访问Servlet API

时间:2016-04-15 02:24:52      阅读:277      评论:0      收藏:0      [点我收藏+]

在struts2中,提供了名称为ActionContext的类来获得ServletApi。创建ActionContext 类对象的语法格式如下:

ActionContext ac=ActionContext.getContext();

下面创建一个实例,演示struts2中的Action是如何通过ActionContext访问servletApi的。该实例的作用就是从网页客户端传送数据过来,通过action的处理,再把处理好的数据传送在客户端网页上显示。

具体步骤如下:

第一步:在项目中创建ServletApiAction的类,代码如下:

package cn.test.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ServletApiAction extends ActionSupport{
?private String username;
?private String password;
?private String sex;
?private String age;
?public String addUI() throws Exception{
??return "addUI";
?}
?@Override
?public String execute() throws Exception {
??// TODO Auto-generated method stub
??ActionContext ac=ActionContext.getContext();
??ac.getApplication().put("username", username);
??ac.getApplication().put("password",password);
??ac.getApplication().put("sex",sex);
??ac.getParameters().put("age", age);
??return "addSuccess";
?}
?public String getUsername() {
??return username;
?}
?public void setUsername(String username) {
??this.username = username;
?}
?public String getPassword() {
??return password;
?}
?public void setPassword(String password) {
??this.password = password;
?}
?public String getSex() {
??return sex;
?}
?public void setSex(String sex) {
??this.sex = sex;
?}
?public String getAge() {
??return age;
?}
?public void setAge(String age) {
??this.age = age;
?}
}

第二步:配置struts.xml文件

? <action name="*" class="cn.test.action.ServletApiAction" method="{1}">
??????? <result name="addSuccess">/WEB-INF/jsp/addSuccess.jsp</result>
??????? <result name="addUI">/WEB-INF/jsp/addUI.jsp</result>
??????? </action>

第三步:创建addUI.jsp,代码如下

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="execute">
<s:textfield name="username" label="用户名" size="15"></s:textfield>
<s:textfield name="age" label="年龄" size="15"></s:textfield>
<s:password name="password" label="密码" size="15"></s:password>
<s:radio list="#{‘男‘:‘男‘,‘女‘:‘女‘}" name="sex" lable="性别" listKey="key" listValue="value"></s:radio>
<s:submit value="添加用户" align="center"></s:submit>
</s:form>
</html>

第四步:创建addSuccess.jsp,代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib? prefix="s" uri="/struts-tags" %>
<table cellpadding="0" cellspacing="0" width="20%" align="center">
? <tr>
??? <th>用户名</th>
??? <th>密码</th>
??? <th>性别</th>
??? <th>年龄</th>
? </tr>
? <tr>
??? <td><s:property value="#application.username"/></td>
??? <td><s:property value="#application.password"/></td>
??? <td><s:property value="#application.sex"/></td>
??? <td><s:property value="#parameters.age"/></td>
? </tr>
</table>

</html>

第五步:部署项目,启动tomcat,在浏览器中输入:http://localhost:8080/MyWeb/addUI

今天就到这里了

?

struts2开发2--struts2访问Servlet API

原文:http://673390302.iteye.com/blog/2290286

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!