欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2传递参数的三种方式

程序员文章站 2022-03-02 22:45:02
...

转自https://blog.csdn.net/Manchester117/article/details/51543566

1.普通的传值方式

UserActionForCommonParam类

Action类接收三个参数,分别是id,username,content.

  1. package com.struts.action;
  2. public class UserActionForCommonParam {
  3. private int id;
  4. private String username;
  5. private String content;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public String getContent() {
  19. return content;
  20. }
  21. public void setContent(String content) {
  22. this.content = content;
  23. }
  24. // 从前台页面接收到参数后会在这个方法里打印
  25. public String addUser() {
  26. System.out.println("ID: " + this.getId());
  27. System.out.println("Username: " + this.getUsername());
  28. System.out.println("Content: " + this.getContent());
  29. return "success";
  30. }
  31. }

对应的struts.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
  7. <constant name="struts.devMode" value="true"/>
  8. <package name="default" namespace="/" extends="struts-default">
  9. <action name="UserActionForCommonParam" class="com.struts.action.UserActionForCommonParam" method="addUser">
  10. <result name="success">/UserPage.jsp</result>
  11. </action>
  12. </package>
  13. </struts>

前台页面UserPage.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <html>
  4. <head>
  5. <title>UserPage</title>
  6. </head>
  7. <body>
  8. <%--下面的Table用于提交参数(id,username,content)--%>
  9. <form action="UserActionForCommonParam" method="POST">
  10. <table border="1">
  11. <tr>
  12. <td>ID:</td>
  13. <td><input type="text" name="id"/></td>
  14. </tr>
  15. <tr>
  16. <td>Username:</td>
  17. <td><input type="text" name="username"/></td>
  18. </tr>
  19. <tr>
  20. <td>Content:</td>
  21. <td><input type="text" name="content"/></td>
  22. </tr>
  23. <tr>
  24. <td colspan="2" align="center">
  25. <input type="submit" value="提交"/>
  26. </td>
  27. </tr>
  28. </table>
  29. </form>
  30. <br/>
  31. <%--下面的Table用于显示传递回来id,username,content--%>
  32. <table border="1">
  33. <tr>
  34. <td>ID: </td>
  35. <td><input type="text" value="${id}"/></td>
  36. </tr>
  37. <tr>
  38. <td>Username: </td>
  39. <td><input type="text" value="${username}"/></td>
  40. </tr>
  41. <tr>
  42. <td>Content: </td>
  43. <td><input type="text" value="${content}"/></td>
  44. </tr>
  45. </table>
  46. <%--用于查看ValueStack中的传递的值--%>
  47. <s:debug/>
  48. </body>
  49. </html>
在实践后会发现,如果遇到参数非常多的情况,那么就需要在Action类中写非常多的属性以及对应的get/set方法.所以这种方式不太可取.解决问题的方法必然是封装一个JavaBean.这就用到了Strut2的第二种传值方式--DomainModel

2.DomainModel传值

首先要创建一个存储的JavaBean

User类

把id,username,content封装的到一个User类当中.

  1. package com.struts.model;
  2. public class User {
  3. private int id;
  4. private String username;
  5. private String content;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public String getContent() {
  19. return content;
  20. }
  21. public void setContent(String content) {
  22. this.content = content;
  23. }
  24. }

UserActionForDomainModel类

  1. package com.struts.action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. import com.struts.model.User;
  4. public class UserActionForDomainModel extends ActionSupport{
  5. private User user;
  6. public User getUser() {
  7. return user;
  8. }
  9. public void setUser(User user) {
  10. this.user = user;
  11. }
  12. public String addUser() {
  13. System.out.println("ID: " + user.getId());
  14. System.out.println("Username: " + user.getUsername());
  15. System.out.println("Content: " + user.getContent());
  16. return "success";
  17. }
  18. }

对应的struts.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
  7. <constant name="struts.devMode" value="true"/>
  8. <package name="default" namespace="/" extends="struts-default">
  9. <action name="userForDomainModel" class="com.struts.action.UserActionForDomainModel" method="addUser">
  10. <result name="success">/UserPage.jsp</result>
  11. </action>
  12. </package>
  13. </struts>

前台页面UserPage.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <html>
  4. <head>
  5. <title>UserPage</title>
  6. </head>
  7. <body>
  8. <%--要特别注意的是DomainModel的传值方式必须要使用对象.参数方式进行赋值以及取值--%>
  9. <form action="userForDomainModel" method="POST">
  10. <table border="1">
  11. <tr>
  12. <td>ID:</td>
  13. <td><input type="text" name="user.id"/></td>
  14. </tr>
  15. <tr>
  16. <td>Username:</td>
  17. <td><input type="text" name="user.username"/></td>
  18. </tr>
  19. <tr>
  20. <td>Content:</td>
  21. <td><input type="text" name="user.content"/></td>
  22. </tr>
  23. <tr>
  24. <td colspan="2" align="center">
  25. <input type="submit" value="提交"/>
  26. </td>
  27. </tr>
  28. </table>
  29. </form>
  30. <br/>
  31. <%--下面的Table用于显示传递回来id,username,content--%>
  32. <table border="1">
  33. <tr>
  34. <td>ID: </td>
  35. <td><input type="text" value="${user.id}"/></td>
  36. </tr>
  37. <tr>
  38. <td>Username: </td>
  39. <td><input type="text" value="${user.username}"/></td>
  40. </tr>
  41. <tr>
  42. <td>Content: </td>
  43. <td><input type="text" value="${user.content}"/></td>
  44. </tr>
  45. </table>
  46. <%--用于查看ValueStack中的传递的值--%>
  47. <s:debug/>
  48. </body>
  49. </html>
实际上User类不需要显式的实例化,struts会自动帮你实例化,但前提条件是,传值时需要使用对象.参数名的方式进行传递.

除了这种传值方式外,struts2还提供另外一种传值方式.

3.ModelDriven传值

依然要创建User的JavaBean

User类

  1. package com.struts.model;
  2. public class User {
  3. private int id;
  4. private String username;
  5. private String content;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public String getContent() {
  19. return content;
  20. }
  21. public void setContent(String content) {
  22. this.content = content;
  23. }
  24. }

UserActionForModelDriven类

  1. package com.struts.action;
  2. import com.opensymphony.xwork2.ModelDriven;
  3. import com.struts.model.User;
  4. public class UserActionForModelDriven implements ModelDriven<User>{
  5. private User user;
  6. public String addUser() {
  7. System.out.println("ID: " + user.getId());
  8. System.out.println("Username: " + user.getUsername());
  9. System.out.println("Content: " + user.getContent());
  10. return "success";
  11. }
  12. @Override
  13. public User getModel() {
  14. if (user == null) {
  15. user = new User();
  16. }
  17. return user;
  18. }
  19. }
这种方式可以不用在Action类中编写对应的get/set方法,但是需要实例化User类.

前台UserPage.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <html>
  4. <head>
  5. <title>UserPage</title>
  6. </head>
  7. <body>
  8. <form action="userForModelDriven" method="POST">
  9. <table border="1">
  10. <tr>
  11. <td>ID:</td>
  12. <td><input type="text" name="id"/></td>
  13. </tr>
  14. <tr>
  15. <td>Username:</td>
  16. <td><input type="text" name="username"/></td>
  17. </tr>
  18. <tr>
  19. <td>Content:</td>
  20. <td><input type="text" name="content"/></td>
  21. </tr>
  22. <tr>
  23. <td colspan="2" align="center">
  24. <input type="submit" value="提交"/>
  25. </td>
  26. </tr>
  27. </table>
  28. </form>
  29. <br/>
  30. <%--下面的Table用于显示传递回来id,username,content--%>
  31. <table border="1">
  32. <tr><span id="transmark"></span>
  33. <td>ID: </td>
  34. <td><input type="text" value="${id}"/></td>
  35. </tr>
  36. <tr>
  37. <td>Username: </td>
  38. <td><input type="text" value="${username}"/></td>
  39. </tr>
  40. <tr>
  41. <td>Content: </td>
  42. <td><input type="text" value="${content}"/></td>
  43. </tr>
  44. </table>
  45. <%--用于查看ValueStack中的传递的值--%>
  46. <s:debug/>
  47. </body>
  48. </html>

页面还是和普通传值一样.

可以去ValueStack当中观察参数的存储,更透彻的理解Struts2的参数传递.


相关标签: JavaWeb