6.Struts2简单类型数据的接受
简单类型数据的接收在Action类中定义与请求参数同名的属性,即,要定义该属性的set方法,便能够使struts2自动接收请求参数并赋予同名属性。
简单类型数据的接受举例:
新建工程项目,名称为:receive_simple_params。

加载struts的核心jar包
xwork-core-2.1.6.jar: Structs2框架的核心类库
struts2-core-2.1.8.1.jar: XWork类库,Structs2在其上构建
ognl-2.7.3.jar:对象图导航语言(Object Graph Navigation Language),Structs2框架通过其读写对象属性。
freemarker-2.3.15.jar: Structs2的UI标签的模板使用FreeMarker编写
commons-logging-1.0.4.jar: ASF出品的日志包,Structs2框架使用这个日志包来支持Log4J和JDK1.4+的日志记录。
commons-fileupload-1.2.1.jar: 文件上传组件,2.1.6版本后必须加入此文件。
Simple_Params_Action.java源码如下:
package actions;
public class Simple_Params_Action {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String execute(){
System.out.println("---------username=---------"+username);
System.out.println("---------age=---------"+age);
return "success";
}
}
index.jsp页面源码如下:
<%@ page pageEncoding="UTF-8"%>
<html>
<head> <title>注册页面</title> </head> <body>
<form action="simple.action" method="post">
用户名<input type="text" name="username"/><br/>
年龄<input type="text" name="age"/></br>
<input type="submit" value="注册"/>
</form>
</body>
</html>
welcome.jsp源码如下:
<%@ page pageEncoding="utf-8" isELIgnored="false"%> <html>
<head> <title>welcome page</title> </head> <body>
用户名:${username}<br/>
年龄:${age} </body>
</html>
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"> <struts>
<package name="one" extends="struts-default"> <action name="simple" class="actions.Simple_Params_Action">
<result>/welcome.jsp</result>
</action> </package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
部署发布,启动tomcat,输入地址:
http://127.0.0.1:8080/receive_simple_params/
运行截图如下:


6.Struts2简单类型数据的接受的更多相关文章
- 7.Struts2复杂类型数据的接受
复合类型数据的接收 所谓复合类型数据是指,一个JavaBean实例的属性值,而这些值又作为参数传递给Action. Action若要接收这些数据,需要做到以下几点: (1)定义Action时,将该Be ...
- js 对 只包含简单类型数据的对象 为元素 组成的数组 进行去重
/** * 对于由简单类型数据组成的对象为元素组成的数组进行去重操作 * @params {Array} 需要去重的对象数组 * @returns {Array} 去重后的对象数组 */ functi ...
- struts2简单入门-数据校验
数据校验流程 校验数据的方式 重写execute方法在内部写校验代码 public class LoginAdminAction extends ActionSupport { private Use ...
- Struts2+Jquery实现ajax并返回json类型数据
来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...
- 转载:Struts2+Jquery实现ajax并返回json类型数据
摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...
- Struts2学习---简单的数据校验、访问Web元素
1.简单的数据校验 在action里面我们已经给出了一个数据校验: public String execute() { if(user.getUsername().equals("usern ...
- Asp.net HttpWebRequest和HttpWebResponse发送和接受任何类型数据
发送字符串数据发送数据 string strId = "guest"; "; string postData = "userid=" + strId; ...
- JS基础语法---(数据)简单类型和复杂类型
原始数据类型: number, string, boolean, undefined, null, object 基本类型(简单类型), 即值类型: number, string, boolean 复 ...
- Spring的controller接受Date类型数据,接受枚举类型数据
1. Controller接收Date类型的数据 核心使用@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") 来将传递过来的时间字符串 ...
随机推荐
- ant的安装及项目的发布
1.安装ant1) 直接解压apache-ant-1.9.7-bin 2) 在环境变量中配置,ant_home的环境变量在 3) 在命令提示符中测试是否安装成功. 2 项目首次打包1) 写好打包的配置 ...
- Swift 3 and OpenGL on Linux and macOS with GLFW
https://solarianprogrammer.com/2016/11/19/swift-opengl-linux-macos-glfw/ Swift 3 and OpenGL on Linux ...
- poj1002-487-3279(字符串处理)
一,题意: 中文题,不解释!二,思路: 1,处理输入的电话号码 2,排序num[]数组 3,输出三,步骤: 1,消除 -.Q.Z 三种字符,将一个电话号码转化为一个整数存如num[]数组 如:num[ ...
- Windows和Linux都有的Copy-on-write技术
Windows和Linux都有的Copy-on-write技术 MySQL技术内幕Innodb存储引擎第2版 P375 SQL Server2008 实现与维护(MCTS教程)P199 LVM快照技术 ...
- Visual Studio “14” CTP 4
微软发布于10月6日发布了Visual Studio "14"CTP 4,本次发布的更新主要包括:ASP.NET vNext runtime和一些工具的优化(ASP.NET vNe ...
- Lesson 4 An existing trip
Text I have just received a letter from my brother,Tim. He is in Australia. He has been there for si ...
- [LeetCode] Interleaving String - 交织的字符串
题目如下:https://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is form ...
- shell使用攻略
shell 是什么 ~ $ ls /bin/*sh /bin/bash /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh 是什么 kernel shell 命令 ...
- 通过圆形载入View了解自定义View
这是自定义View的第一篇文章,通过制作简单的自定义View来了解自定义View的流程. 自定义View是Android学习和开发中必不可少的一部分.通过自定义View我们可以制作丰富绚丽的控件,自定 ...
- [Hadoop大数据]——Hive初识
Hive出现的背景 Hadoop提供了大数据的通用解决方案,比如存储提供了Hdfs,计算提供了MapReduce思想.但是想要写出MapReduce算法还是比较繁琐的,对于开发者来说,需要了解底层的h ...