Struts2自动获取/设置数据的方式一共分为两种

  • 属性驱动(FieldDriven)
  • 模型驱动(ModelDriven)
  1. 属性驱动

属性又分为两种:

|- 基本数据类型

|- JavaBean属性类型

基本数据类型:实例

 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="get">
姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>
<button type="submit">提交</button>
</form>
</body>
</html>
package com.fuwh.struts;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven; public class HelloAction implements Action{ private String name;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行了HelloAction的默认方法");
return SUCCESS;
} }
 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
欢迎${age }岁的${name }光临
</body>
</html>
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="mypack" extends="struts-default">
<action name="hello" class="com.fuwh.struts.HelloAction">
<result name="success">hello.jsp</result>
</action>
</package>
</struts>

JavaBean属性类型

 package com.fuwh.struts;

 public class User implements java.io.Serializable{

     private static final long serialVersionUID = 1L;

     private String name;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }
 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="get">
姓名;<input type="text" name="user.name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
年龄:<input type="text" name="user.age" size="10px" value="23" readonly="readonly"/>
<button type="submit">提交</button>
</form>
</body>
</html>
 package com.fuwh.struts;

 import com.opensymphony.xwork2.Action;

  public class HelloAction implements Action{ 

    private User user 

    public User getUser() {
return user;
}
public void setUser(User) {
this.user = user;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行了HelloAction的默认方法");
return SUCCESS;
} }
 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
欢迎${user.age }岁的${user.name }光临
</body>
</html>

struts.xml配置

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="mypack" extends="struts-default">
<action name="hello" class="com.fuwh.struts.HelloAction">
<result name="success">hello.jsp</result>
</action>
</package>
</struts>

模型驱动

 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="get">
姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>
<button type="submit">提交</button>
</form>
</body>
</html>
 package com.fuwh.struts;

 import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven; public class HelloAction implements Action,ModelDriven{ private User user=new User(); @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行了HelloAction的默认方法mae");
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
System.out.println("执行了getModel方法");
return this.user;
}
}
 <%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
欢迎${age }岁的${name }光临
</body>
</html>
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="mypack" extends="struts-default">
<action name="hello" class="com.fuwh.struts.HelloAction">
<result name="success">hello.jsp</result>
</action>
</package>
</struts>

Struts处理多值参数

普通类型

普通类型和负责类型一样,可以选择用集合来接收,也可以用数组来接收

复杂类型

package com.fuwh.demo;

public class User {

    private int age;
private String name; public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + "]";
} }
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="show" method="post">
兴趣爱好:<input type="text" name="stu[0].age" value="5">
<input type="text" name="stu[0].name" value="fuwh1"><br/>
<input type="text" name="stu[1].age" value="6">
<input type="text" name="stu[1].name" value="fuwh2"><br/>
<input type="text" name="stu[2].age" value="7">
<input type="text" name="stu[2].name" value="fuwh3"><br/>
<input type="submit">
</form>
</body>
</html>
package com.fuwh.demo;

import java.util.List;

import com.opensymphony.xwork2.Action;

public class Show implements Action{

    private List<User> stu;

    public List<User> getStu() {
System.out.println("execut the get method!");
return stu;
} public void setStu(List<User> stu) {
System.out.println("execute the set method!");
this.stu = stu;
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行了action的默认方法");
System.out.println(stu);
return SUCCESS;
} }
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<!-- 开启debug模式,会自动加载配置文件等等,不用每次更改了配置文件就去重新启动下服务器 -->
<constant name="struts.devMode" value="true" /> <package name="test" extends="struts-default">
<action name="show" class="com.fuwh.demo.Show">
<result name="success">hello.jsp</result>
</action> </package> </struts>
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
学生们:${stu}
</body>
</html>

最后出力结果:

Struts2--属性设置方式的更多相关文章

  1. Css颜色定义的方法汇总color属性设置方式

    颜色的定义方式用rgb()里面带上十进制的数字来定义. color:rgb(211,123,135); 用预定义的颜色名称. color:red; rgba()最后一个参数是不透明度. color:r ...

  2. SpringMVC(十四):SpringMVC 与表单提交(post/put/delete的用法);form属性设置encrypt='mutilpart/form-data'时,如何正确配置web.xml才能以put方式提交表单

    SpringMVC 与表单提交(post/put/delete的用法) 为了迎合Restful风格,提供的接口可能会包含:put.delete提交方式.在springmvc中实现表单以put.dele ...

  3. React属性的3种设置方式

    一. 不推荐用setProps,因为以React的设计思想相悖,推荐以父组件向子组件传递属性的方式 二.3种用法的代码 1.键值对 <!DOCTYPE html> <html lan ...

  4. struts2属性Struts2中属性接收参数中文问题和简单数据验证

    PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 一:如果表单提交数据中有中文时,尽量应用post方式. 需要在Struts. ...

  5. struts2访问ServletAPI方式和获取参数的方式

    一.访问ServletAPI的三种方式 方式1:通过让Action类去实现感知接口. 此时项目依赖:servlet-api.jar. ServletRequestAware:感知HttpServlet ...

  6. table中bordercolor属性设置后最新ie浏览器或firefox中不显示边线,借助table的css来实现边线

    table中的bordercolor属性设置后在最新的ie或者firefox中均不显示边线,table的边线又是常用功能.只能使用css来实现了,更通用,更方便一些. css: ​.ctable{ b ...

  7. .net 使用Json(),maxJsonLength属性设置的值问题

    “使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过了为maxJsonLength属性设置的值” 今天业务找我说线上的国家地区都显示数字(地区ID),而 ...

  8. [转]浅谈jQuery EasyUI的属性设置

    原文地址:http://www.easyui.info/archives/1664.html 对jQuery EasyUI有一定了解的话,应该知道基本上每一个组件都有一个"options&q ...

  9. Activity 属性设置大全

    activity属性设置大全 android:allowTaskReparenting=["true"|"false"] 是否允许activity更换从属的任务 ...

  10. libevent (一) socket属性设置与初始化操作

    socket属性设置与初始化操作 libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,内部使用select.epoll.kqueue等系统调用管理事件机制.著名 ...

随机推荐

  1. win2003 Enterprise x64 Edtion中的DCOM服务找不到WORD应用程序

    打开注册表,找到以下健值 HKEY_CLASSES_ROOT\AppID\{00020906-0000-0000-C000-000000000046} HKEY_CLASSES_ROOT\CLSID\ ...

  2. 管理者与下属谈话的技巧及注意点[持续更新ing]

    1.谈话之前要明确谈话的内容.原则和目的(为什么谈,谈什么,要解决什么事) 2.谈话时间不宜过长,尽量控制在半小时以内,最好在每一个阶段或一个节点的时间上去谈 3.谈话单次内容不能过多,但要捉住重点, ...

  3. Jsoup系列学习(2)-解析html文件

    解析html文件 1.当我们通过发送http请求时,有时候返回结果是一个html格式字符串,你需要从一个网站获取和解析一个HTML文档,并查找其中的相关数据.你可以使用下面解决方法: 使用 Jsoup ...

  4. 将Centos的yum源更换为国内的阿里云源

    阿里云是最近新出的一个镜像源.得益于阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ CentOS系统更换软 ...

  5. 简体中国版文档的Markdown语法

    Markdown文件 注意︰这是简体中国版文档的Markdown语法.如果你正在寻找英语版文档.请参阅Markdown︰ Markdown: Syntax. Markdown: Syntax 概述 哲 ...

  6. monkey之monkey日志分析

    一.初步分析方法:Monkey测试出现错误后,一般的差错步骤为以下几步:1.找到是monkey里面的哪个地方出错2.查看Monkey里面出错前的一些事件动作,并手动执行该动作3.若以上步骤还不能找出, ...

  7. 破解 Windows 下Markdown 编辑器 MarkdownPad 2

    MarkdownPad 是 Windows 平台下一款优秀的 Markdown 编辑器,本文简单介绍 Markdown 以及使用一种方法破解 MarkdownPad 使其升级到专业版.该方法仅限于教育 ...

  8. Statement对象的executeUpdate返回信息

    int num = sta.executeUpdate(String sql);返回的是我们平时操作数据库客户端时控制台显示的影响行数

  9. python的函数

    函数一词起源于数学,但是在编程中的函数和数学中的有很大不同.编程中的函数式组织好的,可重复使用的,用于实现单一功能或相关联功能的代码块. 我们在学习过程中已经使用过一些python内建的函数,如pri ...

  10. Parseval's theorem 帕塞瓦尔定理

    Source: wiki: Parseval's theorem As for signal processing, the power within certain frequency band = ...