Struts2--属性设置方式
Struts2自动获取/设置数据的方式一共分为两种
- 属性驱动(FieldDriven)
- 模型驱动(ModelDriven)
- 属性驱动
属性又分为两种:
|- 基本数据类型
|- 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"/>
年龄:<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"/>
年龄:<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"/>
年龄:<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--属性设置方式的更多相关文章
- Css颜色定义的方法汇总color属性设置方式
颜色的定义方式用rgb()里面带上十进制的数字来定义. color:rgb(211,123,135); 用预定义的颜色名称. color:red; rgba()最后一个参数是不透明度. color:r ...
- SpringMVC(十四):SpringMVC 与表单提交(post/put/delete的用法);form属性设置encrypt='mutilpart/form-data'时,如何正确配置web.xml才能以put方式提交表单
SpringMVC 与表单提交(post/put/delete的用法) 为了迎合Restful风格,提供的接口可能会包含:put.delete提交方式.在springmvc中实现表单以put.dele ...
- React属性的3种设置方式
一. 不推荐用setProps,因为以React的设计思想相悖,推荐以父组件向子组件传递属性的方式 二.3种用法的代码 1.键值对 <!DOCTYPE html> <html lan ...
- struts2属性Struts2中属性接收参数中文问题和简单数据验证
PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 一:如果表单提交数据中有中文时,尽量应用post方式. 需要在Struts. ...
- struts2访问ServletAPI方式和获取参数的方式
一.访问ServletAPI的三种方式 方式1:通过让Action类去实现感知接口. 此时项目依赖:servlet-api.jar. ServletRequestAware:感知HttpServlet ...
- table中bordercolor属性设置后最新ie浏览器或firefox中不显示边线,借助table的css来实现边线
table中的bordercolor属性设置后在最新的ie或者firefox中均不显示边线,table的边线又是常用功能.只能使用css来实现了,更通用,更方便一些. css: .ctable{ b ...
- .net 使用Json(),maxJsonLength属性设置的值问题
“使用JSON JavaScriptSerializer进行序列化或反序列化时出错.字符串的长度超过了为maxJsonLength属性设置的值” 今天业务找我说线上的国家地区都显示数字(地区ID),而 ...
- [转]浅谈jQuery EasyUI的属性设置
原文地址:http://www.easyui.info/archives/1664.html 对jQuery EasyUI有一定了解的话,应该知道基本上每一个组件都有一个"options&q ...
- Activity 属性设置大全
activity属性设置大全 android:allowTaskReparenting=["true"|"false"] 是否允许activity更换从属的任务 ...
- libevent (一) socket属性设置与初始化操作
socket属性设置与初始化操作 libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,内部使用select.epoll.kqueue等系统调用管理事件机制.著名 ...
随机推荐
- iOS开发--弹窗多选、单选框架
最近刚毕业...因为个毕设,都离职了又得重新找工作了,这是之前自己没事开直播写的一个小框架,后来各种趋于稳定后,简单的封装了下,写了个小demo,这里就不废话了,直接贴地址,也不编辑了,自己复制过去看 ...
- 关于Xcode8打印一堆log问题
最近太忙了,一直没时间 写博客,项目基本搞完了,这几天没事多写几篇博客.欢迎加群交流iOS技术,QQ交流群:45992174. 刚装的xcode8,不知道从哪来的一堆log 去除方法:Xcode8-- ...
- java知识总结(更新中)
一.java 数据类型 基本类型(byte.short.int. long. char.float.double.boolean) 数字类型 整数型:byte(8).short(16).int(32) ...
- 原创 C++之常量(一)
1概述 一个C++程序就是一系列数据与操作的集合.当一个C++程序开始运行的时候,与该程序相关的数据就会被加载到内存中.当数据与内存发生关联的时候,这些数据就会具有如下的特性: 数据在内存中的地址.这 ...
- hadoop-2.7.1伪分布环境搭建
1.准备Linux环境 1.0 点击VMware快捷方式,右键打开文件所在位置 -> 双击vmnetcfg.exe -> VMnet1 host-only ->修改subnet i ...
- mongodb高级应用
一. 高级查询 查询操作符 条件操作符:db.collection.find({"field":{$gt/$lt/$gte/$lte/$eq/$ne:value}}); 匹配所有 ...
- Azure机器学习入门(二)创建Azure机器学习工作区
我们将开始深入了解如何使用Azure机器学习的基本功能,帮助您开始迈向Azure机器学习的数据科学家之路. Azure ML Studio (Azure Machine Learning Studio ...
- Spring 事务详解
实现购买股票案例: 一.引入JAR文件: 二.开始搭建分层架构---创建账户(Account)和股票(Stock)实体类 Account: ? 1 2 3 4 5 6 7 8 9 10 11 12 1 ...
- angularJS(3)
angularJS(3) 一.angularJs的指令模型ng-model指令 ng-model 指令 绑定 HTML 元素 到应用程序数据. 为应用程序数据提供类型验证(number.email ...
- PHP的GD库
GD库 PHP通过GD库,可以对JPG.PNG.GIF.SWF等图片进行处理.GD库常用在图片加水印,验证码生成等方面. 绘制线条 要对图形进行操作,首先要新建一个画布,通过imagecreatetr ...