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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Struts中Action三种接收参数的方式?

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

前言:

前面已經有一篇隨筆介紹了Struts2的大概原理。本文就Struts2中Action與jsp頁面進行數據對接時介紹幾種常見方法!

?

  • 值棧ValueStack

  • 3個Action?

    Action1

    package com.gdufe.action;import com.opensymphony.xwork2.ActionSupport;/** Action接收參數之后通過set方法賦給普通變量age,name;*/ public class UserAction1 extends ActionSupport{private int age;private String name;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String execute(){return SUCCESS;}public String test(){System.out.println(age +"|"+ name);return SUCCESS;} } View Code

    ?

    ?

    Action2

    package com.gdufe.action;import com.gdufe.pojo.User; import com.opensymphony.xwork2.ActionSupport;/** Action接收參數之后賦給引用對象“user”,內部是set方法賦值*/ public class UserAction2 extends ActionSupport {private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String test(){System.out.println(user.getName() + "|" + user.getAge());return "success";} } View Code

    ?

    ?

    Action3

    package com.gdufe.action;import com.gdufe.pojo.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 test(){System.out.println(user.getName() + "|" + user.getAge());return "success";}public User getModel() {return user;} } View Code

    ?

  • 2個頁面

  • ? ? ? ? ?index.jsp

      

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% 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>My JSP 'index.jsp' starting page</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"></head><body>

        <h2>Action傳值測試</h2>
        <a href="userAction1!test?age=8&name=admin">test1:Attribution</a> <br>
        <a href="userAction2!test?user.age=8&user.name=admin">test2:JavaBean</a> <br>
        <a href="userAction3!test?age=8&name=admin">test3:ModelDriven</a> <br>

    </body> </html>

    ?

        success.jsp

      

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% 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>My JSP 'index.jsp' starting page</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"></head><body><h2>Action傳值雙擊debug</h2><s:debug></s:debug> <!-- debug重要的strut2標簽調試工具 --></body> </html>

    ?

  • 1個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><!-- devMode設置為開發模式 --><constant name="struts.devMode" value="true" /><package name="default" extends="struts-default"><!-- 注:因為Action采用DMI方式,故不需要指明method 以及 ‘result’ --><action name="userAction1" class="com.gdufe.action.UserAction1" ><result>/success.jsp</result></action><action name="userAction2" class="com.gdufe.action.UserAction2" ><result>/success.jsp</result></action><action name="userAction3" class="com.gdufe.action.UserAction3" ><result>/success.jsp</result></action></package></struts>

    ?

      

      運行結果:

    ? ? ?對應Action1——------------------*-------------------------

      對應Action2——------------------*-------------------------

      對應Action3——------------------*-------------------------

    ?

    注意:新手的話請勿按部就班,因為還有初始配置沒有說明,比如jar包及web.xml配置。詳細配置自己讀manual幫助文檔或者上網參考!

    ==============================

    結語:近期在接手Web開發時,數據對接不熟練;鑒于此,才再次翻起struts2的一些基礎知識加深理解。希望能給有打算從事Java的朋友些許借鑒!

      

    轉載于:https://www.cnblogs.com/SeaSky0606/p/4694777.html

    總結

    以上是生活随笔為你收集整理的Struts中Action三种接收参数的方式?的全部內容,希望文章能夠幫你解決所遇到的問題。

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