struts2注入类
struts2是能够注入一个对象的,那么一定须要继承ModelDriven的泛型接口。
package com.test.action; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.test.model.Contact;
import com.test.model.User; public class ValideAction extends ActionSupport implements ModelDriven<User>{ /**
*
*/
private static final long serialVersionUID = 1L; private User user;
private String username;
private Contact contact; public Contact getContact() {
return contact;
} public void setContact(Contact contact) {
this.contact = contact;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} private String password; public String execute()
{
System.out.println(user.getContact().getPhone());
return SUCCESS;
} @Override
public User getModel() {
// TODO Auto-generated method stub
if (null == user) {
return user = new User();
}
return user;
} }
如上面的代码所看到的,就是会将User注入到类action中。而User也是一个嵌套类。
User
package com.test.model; public class User {
private String username; private Contact contact; public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password; public String toString()
{
return username+" "+password;
} }
里面包括一个联系信息类Contact
package com.test.model; public class Contact {
private String phone;
private String address;
private String email; public Contact()
{ }
public Contact(String phone,String address,String email)
{
this.phone=phone;
this.address=address;
this.email=email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} }
然后JSP页面的input类型例如以下所看到的
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 这个地方是用来显示错误信息 -->
<s:fielderror/>
<form action="data.action" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
-------------------联系方式----------------<br>
手机:<input type="text" name="contact.phone"><br>
地址:<input type="text" name="contact.address"><br>
邮箱:<input type="text" name="contact.email"><br> <input type="submit" name="ok"><br>
</form>
</body>
</html>
输出JSP页面例如以下所看到的
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
測试结果页面<br>
用户名:<s:property value="username"/><br>
密码:<s:property value="password"/><br>
--------------------联系方式-------------<br>
手机:<s:property value="contact.phone"/><br>
地址:<s:property value="contact.address"/><br>
邮箱:<s:property value="contact.email"/><br> <s:debug></s:debug>
</body>
</html>
终于的输出结果如图所看到的
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRidWx1b2dl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
从这个样例中。学会了跳跃性的解决这个问题的思路。另外就是对依赖注入还不是很了解,这个须要重点去学习和理解一下,特别是经常使用的几种注入方式。
struts2注入类的更多相关文章
- 简单实用的PHP防注入类实例
这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下 本文实例讲述了简单实用的PHP防注 ...
- springboot管理类,springboot注入类
springboot管理类,springboot注入类 定义一个配置类,添加@Configuration注解,EvaluatorTemplate代表你需要注入的第三方类 @Configuration ...
- springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected single错误
在springmvc中注入服务时用@Service 当有两个实现类时都标明@Service后则会出现异常: nested exception is org.springframework.beans. ...
- Spring_02 注入类型值、利用引用注入类型值、spring表达式、与类相关的注解、与依赖注入相关的注解、注解扫描
注意:注入基本类型值在本质上就是依赖注入,而且是利用的set方式进行的依赖注入 1 注入基本类型的值 <property name="基本类型的成员变量名" value=&q ...
- spring类扫描注入-----类扫描的注解解析器
通过类扫描注入到容器中,这种方式,在实际开发中还是很常用的,可以看下自己的配置文件,就会发现,自己公司的项目,搞不好就是这么注入的. 起码,我发现我公司的项目就是这么干的. 下面来演示一下简单的例子: ...
- c#.net全站防止SQL注入类的代码
using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...
- JAVA框架Struts2 Action类
一.Action书写方式: 接口地址:https://struts.apache.org/maven/struts2-core/apidocs/index.html Action类就是一个POJO类. ...
- .net 过滤 sql防注入类,省地以后每次都要重新弄!
/// <summary> /// 过滤不安全的字符串 /// </summary> /// <param name="Str"&g ...
- Struts2 Action类的创建以及参数传递以及接收
一.Struts中Action得创建方式 1,直接创建一个简单的Action类 添加Struts.xml,配置转发方法返回转发的页面. 2,实现一个Action类 Strust.xml配置对应的Url ...
随机推荐
- C++标准库概述
一.C++标准库的主要组件: 1.标准C库 2.I/O流技术(对标准输入输出设备称为标准I/O,对在外磁盘上文件的输入输出称为文件I/O,对内存中指定的字符串存储空间的输入输出称为串I/O) 3.st ...
- CHARINDEX,REPLACE,LEFT+四大系统函数+模糊查询
select CHARINDEX('bob','my name is bob',1)--返回12 bob的第一个b在字符串中排第12(从1开始数) select CEILING(456.4)--45 ...
- Kali linux查看局域网内其他用户的输入信息
使用nmap 工具在局域网里进行侦探,查看局域网里ip存活数量 root@kali:~# nmap -sP 192.168.1.0/24 Starting Nmap 7.60 ( https://nm ...
- [AngularFire2 & Firestore] Example for collection and doc
import {Injectable} from '@angular/core'; import {Skill} from '../models/skills'; import {AuthServic ...
- ubuntu下sudo命令不再输入密码
ubuntu下普通用户是没有root权限,很多命令在使用时都需要使用命令sudo 'cmd',但系统需要user验证自己,即需要输入普通用户的密码.但普通用户是否有执行该cmd的权限,需要到系统文件/ ...
- LeetCode 136 Single Number(仅仅出现一次的数字)
翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array ...
- BZOJ1576: [Usaco2009 Jan]安全路经Travel(树链剖分)
Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...
- Office2010激活工具
mini-KMS Activator是一款好用Office2010激活工具,“KMS”是微软对于“大客户”(VOL或VL)提供的Microsoft产品激活方式之一.在适用此方式的Microsoft产品 ...
- 【Codeforces Round #429 (Div. 2) A】Generous Kefa
[Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...
- ArcGIS 点要素新增点
IFeatureLayer layer = FrmMain.m_mapControl.get_Layer(0) as IFeatureLayer; IFeatureClass featureClass ...