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. Play Framework 项目遇到问题

    1.Debug调试出错,提示: Error occurred during initialization of VMagent library failed to init: jdwpERROR: C ...

  2. 基于Spring+SpringMVC+Mybatis的Web系统搭建

    系统搭建的配置大同小异,本文在前人的基础上做了些许的改动,重写数据库,增加依据权限的动态菜单的实现,也增加了后台返回json格式数据的配置,详细参见完整源码. 主要的后端架构:Spring+Sprin ...

  3. Java 异常处理

    异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...

  4. Yii2 脚本手动执行可以,计划任务执行不成功

    用Yii2的console写了个脚本,在命令行执行都OK. 放到cron里面也按时去执行了,但就是执行的效果不对,console脚本执行结果不对. 查看之后的是由于yii脚本的php路径问题(跟目录下 ...

  5. DeprecatedAttribute vs. ObsoleteAttribute

    定义比较 ObsoleteAttribute [SerializableAttribute] [AttributeUsageAttribute(AttributeTargets.Class | Att ...

  6. MySql in子句 效率低下优化

    MySql in子句 效率低下优化 背景: 更新一张表中的某些记录值,更新条件来自另一张含有200多万记录的表,效率极其低下,耗时高达几分钟. where resid in ( ); 耗时 365s ...

  7. PHP求职宝典系列——PHP Web 编程篇

    PHP Web 编程篇 form表单 1.简述 POST 和 GET 传输的最大容量分别是多少? GET 方法提交的表单数据被附加到 URL 上,并作为URL 的一部分发送到服务器端. URL 的长度 ...

  8. Animator Controller 继承关系

    准备知识 对于Animator Controller中蜘蛛网一样的几十条连线,后续如果靠人工维护,那成本将是很大. AnimatorOverrideController组件的文档:https://do ...

  9. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  10. 跨域请求——WebClient通过get和post请求api

    AJAX不可以实现跨域请求,经过特殊处理才行.一般后台可以通过WebClient实现跨域请求~ //get 请求        string url = string.Format("htt ...