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等系统调用管理事件机制.著名 ...
随机推荐
- 使用GIT进行源码管理 —— 在VisualStudio中使用GIT
GIT作为源码管理的方式现在是越来越流行了,在VisualStudio 2012中,就通过插件的现实对GIT进行了官方支持,并且这个插件在VS2013中已经转正.本文在这里简单的介绍一下如何在Visu ...
- 如何写出高质量的技术博客 这边文章出自http://www.jianshu.com/p/ae9ab21a5730 觉得不错直接拿过来了 好东西要大家分享嘛
如何写出高质量的技术博客?答案是:如果你想,就一定能写出高质量的技术博客.看起来很唯心,但这就是事实.有足够愿力去做一件目标明确,有良好反馈系统的事情往往很简单.就是不停地训练,慢慢地,你自己 ...
- Play Framework 完整实现一个APP(十三)
添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...
- 在DBeaver中phoenix查询报错:org.apache.phoenix.exception.PhoenixIOException: The system cannot find the path specified
环境:Phoenix:4.4,win7系统 问题:Phoenix在查询hbase时,报"系统找不到指定路径". 解决: 请参见 https://distcp.quora.com/C ...
- 微信app支付 ci框架做的
/** * 组合微信app支付 获得prepayid * @param int $order_num */ private function _wxpay_reques ...
- HAProxy介绍
简单说明 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需 ...
- [No0000A6]Visual Studio 2015 中的常用命令的默认键盘快捷键-VS2015 Shortcut
注意:你也可以通过打开"选项"对话框,展开"环境"节点,然后选择"键盘",查找任何命令的快捷键. Build(生成) 命令 键盘快捷键 [上 ...
- POJ 1979 Red and Black
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #inc ...
- mybatis一对多关联
这里的一对多指的是:当我们查询一个对象的时候,同时将其有关联的多方对象都查询出来. 下面以国家(Country)和部长(Minsiter)做案例 一个国家有多个部长 1.定义实体 定义实体的时候需要注 ...
- vue.js学习(第一课)
学习资料 来自台湾小凡! vue.js是javascript的一个库,只专注于UI层面,核心价值永远是 API的简洁. 第一课: 不支持IE8. 1.声明式渲染: el元素的简称 element : ...