struts2是能够注入一个对象的,那么一定须要继承ModelDriven的泛型接口。

  1. package com.test.action;
  2.  
  3. import com.opensymphony.xwork2.ActionSupport;
  4. import com.opensymphony.xwork2.ModelDriven;
  5. import com.test.model.Contact;
  6. import com.test.model.User;
  7.  
  8. public class ValideAction extends ActionSupport implements ModelDriven<User>{
  9.  
  10. /**
  11. *
  12. */
  13. private static final long serialVersionUID = 1L;
  14.  
  15. private User user;
  16. private String username;
  17. private Contact contact;
  18.  
  19. public Contact getContact() {
  20. return contact;
  21. }
  22.  
  23. public void setContact(Contact contact) {
  24. this.contact = contact;
  25. }
  26.  
  27. public String getUsername() {
  28. return username;
  29. }
  30.  
  31. public void setUsername(String username) {
  32. this.username = username;
  33. }
  34.  
  35. public String getPassword() {
  36. return password;
  37. }
  38.  
  39. public void setPassword(String password) {
  40. this.password = password;
  41. }
  42.  
  43. private String password;
  44.  
  45. public String execute()
  46. {
  47. System.out.println(user.getContact().getPhone());
  48. return SUCCESS;
  49. }
  50.  
  51. @Override
  52. public User getModel() {
  53. // TODO Auto-generated method stub
  54. if (null == user) {
  55. return user = new User();
  56. }
  57. return user;
  58. }
  59.  
  60. }

如上面的代码所看到的,就是会将User注入到类action中。而User也是一个嵌套类。

User

  1. package com.test.model;
  2.  
  3. public class User {
  4. private String username;
  5.  
  6. private Contact contact;
  7.  
  8. public Contact getContact() {
  9. return contact;
  10. }
  11. public void setContact(Contact contact) {
  12. this.contact = contact;
  13. }
  14. public String getUsername() {
  15. return username;
  16. }
  17. public void setUsername(String username) {
  18. this.username = username;
  19. }
  20. public String getPassword() {
  21. return password;
  22. }
  23. public void setPassword(String password) {
  24. this.password = password;
  25. }
  26. private String password;
  27.  
  28. public String toString()
  29. {
  30. return username+" "+password;
  31. }
  32.  
  33. }

里面包括一个联系信息类Contact

  1. package com.test.model;
  2.  
  3. public class Contact {
  4. private String phone;
  5. private String address;
  6. private String email;
  7.  
  8. public Contact()
  9. {
  10.  
  11. }
  12. public Contact(String phone,String address,String email)
  13. {
  14. this.phone=phone;
  15. this.address=address;
  16. this.email=email;
  17. }
  18. public String getPhone() {
  19. return phone;
  20. }
  21. public void setPhone(String phone) {
  22. this.phone = phone;
  23. }
  24. public String getAddress() {
  25. return address;
  26. }
  27. public void setAddress(String address) {
  28. this.address = address;
  29. }
  30. public String getEmail() {
  31. return email;
  32. }
  33. public void setEmail(String email) {
  34. this.email = email;
  35. }
  36.  
  37. }

然后JSP页面的input类型例如以下所看到的

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib uri="/struts-tags" prefix="s"%>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>Insert title here</title>
  10. </head>
  11. <body>
  12. <!-- 这个地方是用来显示错误信息 -->
  13. <s:fielderror/>
  14. <form action="data.action" method="post">
  15. 用户名:<input type="text" name="username"><br>
  16. 密码:<input type="password" name="password"><br>
  17. -------------------联系方式----------------<br>
  18. 手机:<input type="text" name="contact.phone"><br>
  19. 地址:<input type="text" name="contact.address"><br>
  20. 邮箱:<input type="text" name="contact.email"><br>
  21.  
  22. <input type="submit" name="ok"><br>
  23. </form>
  24. </body>
  25. </html>

输出JSP页面例如以下所看到的

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib uri="/struts-tags" prefix="s" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. 測试结果页面<br>
  12. 用户名:<s:property value="username"/><br>
  13. 密码:<s:property value="password"/><br>
  14. --------------------联系方式-------------<br>
  15. 手机:<s:property value="contact.phone"/><br>
  16. 地址:<s:property value="contact.address"/><br>
  17. 邮箱:<s:property value="contact.email"/><br>
  18.  
  19. <s:debug></s:debug>
  20. </body>
  21. </html>

终于的输出结果如图所看到的

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRidWx1b2dl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

从这个样例中。学会了跳跃性的解决这个问题的思路。另外就是对依赖注入还不是很了解,这个须要重点去学习和理解一下,特别是经常使用的几种注入方式。

struts2注入类的更多相关文章

  1. 简单实用的PHP防注入类实例

    这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下   本文实例讲述了简单实用的PHP防注 ...

  2. springboot管理类,springboot注入类

    springboot管理类,springboot注入类 定义一个配置类,添加@Configuration注解,EvaluatorTemplate代表你需要注入的第三方类 @Configuration ...

  3. springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected single错误

    在springmvc中注入服务时用@Service 当有两个实现类时都标明@Service后则会出现异常: nested exception is org.springframework.beans. ...

  4. Spring_02 注入类型值、利用引用注入类型值、spring表达式、与类相关的注解、与依赖注入相关的注解、注解扫描

    注意:注入基本类型值在本质上就是依赖注入,而且是利用的set方式进行的依赖注入 1 注入基本类型的值 <property name="基本类型的成员变量名" value=&q ...

  5. spring类扫描注入-----类扫描的注解解析器

    通过类扫描注入到容器中,这种方式,在实际开发中还是很常用的,可以看下自己的配置文件,就会发现,自己公司的项目,搞不好就是这么注入的. 起码,我发现我公司的项目就是这么干的. 下面来演示一下简单的例子: ...

  6. c#.net全站防止SQL注入类的代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  7. JAVA框架Struts2 Action类

    一.Action书写方式: 接口地址:https://struts.apache.org/maven/struts2-core/apidocs/index.html Action类就是一个POJO类. ...

  8. .net 过滤 sql防注入类,省地以后每次都要重新弄!

    /// <summary>    /// 过滤不安全的字符串    /// </summary>    /// <param name="Str"&g ...

  9. Struts2 Action类的创建以及参数传递以及接收

    一.Struts中Action得创建方式 1,直接创建一个简单的Action类 添加Struts.xml,配置转发方法返回转发的页面. 2,实现一个Action类 Strust.xml配置对应的Url ...

随机推荐

  1. VS中创建自定义一个VC工程为基础的开发向导的总结

    作者:朱金灿 来源:http://blog.csdn.net/clever101 VS允许用户进行自定义开发向导.自定义开发向导的好处在于将常用的设置都通过向导生成,从而大大提供开发效率.特别是在开发 ...

  2. Kinect 开发 —— 手势识别(下)

    基本手势追踪 手部追踪在技术上和手势识别不同,但是它和手势识别中用到的一些基本方法是一样的.在开发一个具体的手势控件之前,我们先建立一个可重用的追踪手部运动的类库以方便我们后续开发.这个手部追踪类库包 ...

  3. colrm---删除文件制定列

  4. RecyclerView具体解释

    public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild { 由上面的继承结 ...

  5. UVA - 10674-Tangents

     题意:给出两个圆,求它们的公切线,并依照一定格式输出 做法:模拟 代码: #include<iostream> #include<map> #include<str ...

  6. 搜索 debian8.7.1 ,google vs baidu

    国外的 Linux 比国内流行, debian官方网站只能找到当前版本DVD文件.想找旧版的Debian在百度一圈后徒劳无功,于是把目标转向 google ,只需要输入 debian?8.7.1-i3 ...

  7. 有關AWS EC2 (EBS 收費)的問題

    有關AWS EC2 (EBS 收費)的問題 之前一陣子的時候,公司在使用Amazone Web Service (AWS)的 EC2 (Amazon Elastic Compute Cloud).不過 ...

  8. [51Nod]NOIP2018提高组省一冲奖班模测训练(二)

    http://www.51nod.com/contest/problemList.html#!contestId=73&randomCode=4408520896354389006 还是原题大 ...

  9. Android控件开发之Gallery3D效果

    package xiaosi.GalleryFlow; import android.app.Activity; import android.os.Bundle; public class Gall ...

  10. Python 极简教程(一)前言

    现在 Python 用处很多,学的人也很多,其流行程度自不必说.但是很多人学 Python 的时候都遇到过问题,特别对于非计算机专业毕业的人来说. 现在的教程非常多,但是绝大部分对于初学者都不够友好. ...