20.struts2的数据填充和类型转换.md
目录
1. struts2的自动填充
当jsp和Action类中对象名称一致时候,拦截器会自动拦截填充。
拦截器:
Demo:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/user_register.action" method="post">
用户名:<input type="text" name="name"><br/>
年 龄:<input type="text" name="age"><br/>
生 日:<input type="text" name="birth"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
package per.liyue.struts2_datafomat;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/*
* 注意:一定要提供Setter方法
*/
private String name;
private int age;
private Date birth;
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;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String register(){
System.out.println(name);
System.out.println(age);
System.out.println(birth);
return "register";
}
}
<?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="data_conversion" extends="struts-default" >
<!-- 使用通配符实现 -->
<action name="user_*" class="per.liyue.struts2_datafomat.UserAction" method="{1}">
<result name="{1}">/user.jsp</result>
</action>
</package>
</struts>
<?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>
<!-- 在总配置文件中引入其他配置文件 -->
<include file="per/liyue/code/struts2demo/config_HelloStruts2.xml"></include>
<include file="per/liyue/code/struts2_data/data.xml"></include>
<include file="per/liyue/struts2_datafomat/data_conversion.xml"></include>
</struts>
在浏览器地址栏输入:
http://localhost.:8080/StrutsDemo1/user_register.action
填写后提交,得到输入内容
2. struts2的对象填充
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/user_register.action" method="post">
用户名:<input type="text" name="user.name"><br/>
年 龄:<input type="text" name="user.age"><br/>
生 日:<input type="text" name="user.birth"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
package per.liyue.struts2_datafomat;
import java.util.Date;
public class User {
/*
* 注意:一定要提供Setter方法
*/
private String name;
private int age;
private Date birth;
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;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
package per.liyue.struts2_datafomat;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/*
* 注意:提供setter和getter方法
*/
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String register(){
System.out.println(user.getName());
System.out.println(user.getAge());
System.out.println(user.getBirth());
return "register";
}
}
其他保持不变,浏览器地址栏输入:
http://localhost.:8080/StrutsDemo1/user_register.action
填写后提交,得到输入内容.
3. struts2的类型转换器
对于一些没有按照指定格式输入的内容,那么可以用自定义的类型转换器来处理:
3.1 类继承关系
---TypeConverter
------DefaultTypeConverter
---------StrutsTypeConverter
3.2 局部转换器
步骤:
编写转换器类:继承转换类
在包下建立属性文件,文件名称严格按照格式
- Action名称-conversion.properties
- 然后配置:需要转换的字段名=自定义转换器类的权限名称
例如birthday=cn.itcast.convertor.DateTypeConvertor
3.3 全局转换器
步骤:
- 继承转换类,实现抽象方法
- 在包下建立属性文件,严格按照格式
- xwork-conversion.properties
3.4 注意
二者可以同时共用,但是同时出现时候优先局部转换器
20.struts2的数据填充和类型转换.md的更多相关文章
- Struts2基础数据校验和框架校验
一:三种校验的方式 1.用validate()方法实现数据校验 2.用execute()方法实现数据校验 3.用validateXxx()方法实现数据校验 1.用validate()方法实现数据校验 ...
- (转)struts2:数据校验,通过XWork校验框架实现(validation.xml)
转载自:http://www.cnblogs.com/nayitian/p/3475661.html struts2:数据校验,通过XWork校验框架实现(validation.xml) 根据输入 ...
- .net dataGridView当鼠标经过时当前行背景色变色;然后【给GridView增加单击行事件,并获取单击行的数据填充到页面中的控件中】
1.首先在前台dataGridview属性中增加onRowDataBound属性事件 2.然后在后台Observing_RowDataBound事件中增加代码 protected void Obser ...
- struts2:数据校验,通过XWork校验框架实现(validation.xml)
根据输入校验的处理场所的不同,可以将输入校验分为客户端校验和服务器端校验两种.服务器端验证目前有两种方式: 第一种: 参考:struts2:数据校验,通过Action中的validate()方法实现校 ...
- Laravel 5.2 教程 - 数据填充
一.简介 Laravel提供的填充类(seed),可以让大家很容易的实现填充测试数据到数据库.所有的填充类都位于database/seeds目录.填充类的类名完全由你自定义,但最好还是遵循一定的规则, ...
- laravel的工厂模式数据填充:
数据表post中的字段结构. database\factory\UserFactory.php $factory->define(App\Post::class,function (Faker ...
- Laravel 实践之路: 数据库迁移与数据填充
数据库迁移实际上就是对数据库库表的结构变化做版本控制,之前对数据库库表结构做修改的方式比较原始,比如说对某张库表新增了一个字段,都是直接在库表中执行alter table xxx add .. 的方式 ...
- jQuery.fill 数据填充插件
博客园的伙伴们,大家好,I'm here,前段时间特别的忙,只有零星分散的时间碎片,有时仰望天空,有时发呆,有时写代码,正如下面给大家介绍的这个jQuery.fill插件,正是在这样的状态下写出来的. ...
- 时间格式的处理和数据填充和分页---laravel
时间格式文档地址:http://carbon.nesbot.com/docs/ 这是些时间格式,只需要我们这么做就可以 我们在模板层,找到对应的模型对象那里进行处理就可以啦 2018-11-08 16 ...
随机推荐
- tips:可变参数列表
tips:可变参数列表! 先来看看以往我们要传递许多参数时是怎么做的: java: public static void main(String []args){} c: int main(int a ...
- Visual Studio 2012 & MyEclipse2015 快捷键对比
- Mysql 64位解压版的安装
先下载解压版的mysql 下载地址 https://dev.mysql.com/downloads/file/?id=474496 解压 进到里面新建这个文件夹和文件 打开my.ini文件(用文本编辑 ...
- jquery遍历指定元素下的img图片改变其路径
$(".jieshao img").each(function (i) { $(this).attr("src", "manager/" + ...
- Java - 23 Java 抽象类
Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不 ...
- DRF 视图组件,路由组件
视图组件 -- 第一次封装 -- GenericAPIView(APIView): queryset = None serializer_class = None def ge ...
- 安全测试1_Web知识简介
接下去所有的安全测试都是本人学习安全测试的过程,随笔中会截取云课堂视频中的图片(比较生动和形象,便于理解),主要目的是方便自己以后复习和巩固! 1.Web发展阶段概述: 2.web安全我能提发展形势: ...
- jdbc(MySQL)
1.连接数据库 2.使用配置文件 3.启用连接池 4.事务 JDBC WHAT? 用于执行 SQL 语句的 Java API WHY? 不需要了解每一种数据库连接操作方式 HOW? 加载驱动.获取连接 ...
- java.net.BindException: 地址已在使用 (Bind failed)
java.net.BindException: 地址已在使用,是因为端口被占用,出现在启动服务的时候 报错如截图 报错显示 10062端口被占用冲突 执行netstat -alnp | grep 10 ...
- scrapy之如何迭代spider 的parse函数生成器
Engine.py (core): d = self.scraper.enqueue_scrape(response, request, spider) def _handle_down ...