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 ...
随机推荐
- VS中创建自定义一个VC工程为基础的开发向导的总结
作者:朱金灿 来源:http://blog.csdn.net/clever101 VS允许用户进行自定义开发向导.自定义开发向导的好处在于将常用的设置都通过向导生成,从而大大提供开发效率.特别是在开发 ...
- Kinect 开发 —— 手势识别(下)
基本手势追踪 手部追踪在技术上和手势识别不同,但是它和手势识别中用到的一些基本方法是一样的.在开发一个具体的手势控件之前,我们先建立一个可重用的追踪手部运动的类库以方便我们后续开发.这个手部追踪类库包 ...
- colrm---删除文件制定列
- RecyclerView具体解释
public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild { 由上面的继承结 ...
- UVA - 10674-Tangents
题意:给出两个圆,求它们的公切线,并依照一定格式输出 做法:模拟 代码: #include<iostream> #include<map> #include<str ...
- 搜索 debian8.7.1 ,google vs baidu
国外的 Linux 比国内流行, debian官方网站只能找到当前版本DVD文件.想找旧版的Debian在百度一圈后徒劳无功,于是把目标转向 google ,只需要输入 debian?8.7.1-i3 ...
- 有關AWS EC2 (EBS 收費)的問題
有關AWS EC2 (EBS 收費)的問題 之前一陣子的時候,公司在使用Amazone Web Service (AWS)的 EC2 (Amazon Elastic Compute Cloud).不過 ...
- [51Nod]NOIP2018提高组省一冲奖班模测训练(二)
http://www.51nod.com/contest/problemList.html#!contestId=73&randomCode=4408520896354389006 还是原题大 ...
- Android控件开发之Gallery3D效果
package xiaosi.GalleryFlow; import android.app.Activity; import android.os.Bundle; public class Gall ...
- Python 极简教程(一)前言
现在 Python 用处很多,学的人也很多,其流行程度自不必说.但是很多人学 Python 的时候都遇到过问题,特别对于非计算机专业毕业的人来说. 现在的教程非常多,但是绝大部分对于初学者都不够友好. ...