日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Struts2中action接受参数方法

發布時間:2023/12/10 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2中action接受参数方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Struts2中Action接收參數的方法主要有以下三種:
1.使用Action的屬性接收參數:
? ?a.定義:在Action類中定義屬性,創建get和set方法;
? ?b.接收:通過屬性接收參數,如:userName;
? ?c.發送:使用屬性名傳遞參數,如:user1!add?userName=Magci;
2.使用DomainModel接收參數:
? ?a.定義:定義Model類,在Action中定義Model類的對象(不需要new),創建該對象的get和set方法;
? ?b.接收:通過對象的屬性接收參數,如:user.getUserName();
? ?c.發送:使用對象的屬性傳遞參數,如:user2!add?user.userName=MGC;
3.使用ModelDriven接收參數:
? ?a.定義:Action實現ModelDriven泛型接口,定義Model類的對象(必須new),通過getModel方法返回該對象;
? ?b.接收:通過對象的屬性接收參數,如:user.getUserName();
? ?c.發送:直接使用屬性名傳遞參數,如:user2!add?userName=MGC;

實例:

web.xml:


Java代碼
  • <?xml version="1.0" encoding="UTF-8"?> ?

  • <web-app version="2.5"

  • ? ?xmlns="http://java.sun.com/xml/ns/javaee"

  • ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  • ? ?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ?

  • ? ?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  • ?<welcome-file-list> ?

  • ? ?<welcome-file>hello.jsp</welcome-file> ?

  • ?</welcome-file-list> ?

  • ?<filter> ?

  • ? ?<filter-name>struts2</filter-name> ?

  • ? ?<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> ?

  • ?</filter> ?

  • ?<filter-mapping> ?

  • ? ?<filter-name>struts2</filter-name> ?

  • ? ?<url-pattern>/*</url-pattern> ?

  • ?</filter-mapping> ?

  • </web-app> ?

  • [java]view plaincopy
  • <?xml version="1.0" encoding="UTF-8"?> ?

  • <web-app version="2.5"

  • ? ?xmlns="http://java.sun.com/xml/ns/javaee"

  • ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  • ? ?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

  • ? ?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  • ?<welcome-file-list> ?

  • ? ?<welcome-file>hello.jsp</welcome-file> ?

  • ?</welcome-file-list> ?

  • ?<filter> ?

  • ? ?<filter-name>struts2</filter-name> ?

  • ? ?<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> ?

  • ?</filter> ?

  • ?<filter-mapping> ?

  • ? ?<filter-name>struts2</filter-name> ?

  • ? ?<url-pattern>/*</url-pattern> ?

  • ?</filter-mapping> ?

  • </web-app> ?


  • struts.xml:


    寫道<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

    <struts>
    <!--
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="example.xml"/>



    <package name="default" namespace="/" extends="struts-default">
    <default-action-ref name="index" />
    <action name="index">
    <result type="redirectAction">
    <param name="actionName">HelloWorld</param>
    <param name="namespace">/example</param>
    </result>
    </action>
    </package>
    -->

    <!-- Add packages here -->
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/" extends="struts-default">
    <action name="user*" class="cn.edu.ahau.mgc.struts2.action.UserAction{1}">
    <result>/addSuccess.jsp</result>
    </action>
    </package>
    </struts>


    User.java:


    Java代碼
  • package cn.edu.ahau.mgc.struts2.mode; ?

  • publicclass User { ?

  • private String userName; ?

  • private String password; ?

  • public String getUserName() { ?

  • returnthis.userName; ?

  • ? ?} ?

  • publicvoid setUserName(String userName) { ?

  • this.userName = userName; ?

  • ? ?} ?

  • public String getPassword() { ?

  • returnthis.password; ?

  • ? ?} ?

  • publicvoid setPassword(String password) { ?

  • this.password = password; ?

  • ? ?} ?

  • } ?

  • [java]view plaincopy
  • package cn.edu.ahau.mgc.struts2.mode; ?

  • publicclass User { ?

  • private String userName; ?

  • private String password; ?

  • public String getUserName() { ?

  • returnthis.userName; ?

  • ? ?} ?

  • publicvoid setUserName(String userName) { ?

  • this.userName = userName; ?

  • ? ?} ?

  • public String getPassword() { ?

  • returnthis.password; ?

  • ? ?} ?

  • publicvoid setPassword(String password) { ?

  • this.password = password; ?

  • ? ?} ?

  • } ?


  • UserAction1.java:


    寫道package cn.edu.ahau.mgc.struts2.action;

    import com.opensymphony.xwork2.ActionSupport;

    public class UserAction1 extends ActionSupport {

    private String userName;
    private String password;

    public String add() {
    System.out.println("userName: " + userName);
    System.out.println("password: " + password);
    return SUCCESS;
    }

    public String getUserName() {
    return this.userName;
    }

    public void setUserName(String userName) {
    this.userName = userName;
    }

    public String getPassword() {
    return this.password;
    }

    public void setPassword(String password) {
    this.password = password;
    }
    }


    UserAction2.java:



    寫道package cn.edu.ahau.mgc.struts2.action;

    import com.opensymphony.xwork2.ActionSupport;

    import cn.edu.ahau.mgc.struts2.mode.User;

    public class UserAction2 extends ActionSupport {

    private User user;

    public String add() {
    System.out.println("userName: " + user.getUserName());
    System.out.println("password: " + user.getPassword());
    return SUCCESS;
    }

    public User getUser() {
    return this.user;
    }

    public void setUser(User user) {
    this.user = user;
    }

    }


    UserAction3.java:



    寫道package cn.edu.ahau.mgc.struts2.action;

    import cn.edu.ahau.mgc.struts2.mode.User;

    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;

    public class UserAction3 extends ActionSupport implements ModelDriven<User> {

    private User user = new User();

    public String add() {
    System.out.println("userName: " + user.getUserName());
    System.out.print("password: " + user.getPassword());
    return SUCCESS;
    }

    public User getModel() {
    return this.user;
    }

    }


    index.jsp:


    寫道<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">

    <title>Param</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    </head>

    <body>
    <a href="user1!add?userName=Magci&password=123456">user1!add?userName=Magci&password=123456</a>
    <br />
    <br />

    <a href="user2!add?user.userName=MGC&user.password=abc">user2!add?user.userName=MGC&user.password=abc</a>
    <br />
    <br />

    <a href="user3!add?userName=MaGC&password=000000">user3!add?userName=MaGC&password=000000</a>
    </body>
    </html>

    addSuccess.jsp:


    Java代碼
  • <%@ page language="java"import="java.util.*" pageEncoding="GB18030"%> ?

  • <% ?

  • String path = request.getContextPath(); ?

  • String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; ?

  • %> ?

  • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ?

  • <html> ?

  • ?<head> ?

  • ? ?<base href="<%=basePath%>"> ?

  • ? ?<title>AddSuccess</title> ?

  • ? ?<meta http-equiv="pragma" content="no-cache"> ?

  • ? ?<meta http-equiv="cache-control" content="no-cache"> ?

  • ? ?<meta http-equiv="expires" content="0"> ? ? ?

  • ? ?<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> ?

  • ? ?<meta http-equiv="description" content="This is my page"> ?

  • ? ?<!-- ?

  • ? ?<link rel="stylesheet" type="text/css" href="styles.css"> ?

  • ? ?--> ?

  • ?</head> ?

  • ?<body> ?

  • ? ?User Add Success! <br> ?

  • ?</body> ?

  • </html> ?


  • 轉載于:https://blog.51cto.com/lebron/1373552

    總結

    以上是生活随笔為你收集整理的Struts2中action接受参数方法的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。