struts2接收前台参数的3个方法

来源:互联网 发布:吉林软件行业协会 编辑:程序博客网 时间:2024/06/10 07:27
Java代码  收藏代码
  1. 01.public class GetRequestParameterAction extends ActionSupport {     
  2. 02.    
  3. 03.    private String bookName;     
  4. 04.    private String bookPrice;     
  5. 05.         
  6. 06.    public String getBookName() {     
  7. 07.        return bookName;     
  8. 08.    }     
  9. 09.    
  10. 10.    public void setBookName(String bookName) {     
  11. 11.        this.bookName = bookName;     
  12. 12.    }     
  13. 13.    
  14. 14.    public String getBookPrice() {     
  15. 15.        return bookPrice;     
  16. 16.    }     
  17. 17.    
  18. 18.    public void setBookPrice(String bookPrice) {     
  19. 19.        this.bookPrice = bookPrice;     
  20. 20.    }     
  21. 21.         
  22. 22.         
  23. 23.    public String  execute() throws Exception{     
  24. 24.             
  25. 25.             
  26. 26.        //方式一: 将参数作为Action的类属性,让OGNL自动填充     
  27. 27.              
  28. 28.        System.out.println("方法一,把参数作为Action的类属性,让OGNL自动填充:");     
  29. 29.        System.out.println("bookName: "+this.bookName);     
  30. 30.        System.out.println("bookPrice: " +this.bookPrice);     
  31. 31.             
  32. 32.             
  33. 33.        //方法二:在Action中使用ActionContext得到parameterMap获取参数:     
  34. 34.        ActionContext context=ActionContext.getContext();     
  35. 35.        Map  parameterMap=context.getParameters();     
  36. 36.             
  37. 37.        String bookName2[]=(String[])parameterMap.get("bookName");     
  38. 38.        String bookPrice2[]=(String[])parameterMap.get("bookPrice");     
  39. 39.             
  40. 40.        System.out.println("方法二,在Action中使用ActionContext得到parameterMap获取参数:");     
  41. 41.        System.out.println("bookName: " +bookName2[0]);     
  42. 42.        System.out.println("bookPrice: " +bookPrice2[0]);     
  43. 43.             
  44. 44.             
  45. 45.        //方法三:在Action中取得HttpServletRequest对象,使用request.getParameter获取参数     
  46. 46.        HttpServletRequest request = (HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);      
  47. 47.              
  48. 48.        String bookName=request.getParameter("bookName");     
  49. 49.        String bookPrice=request.getParameter("bookPrice");     
  50. 50.             
  51. 51.        System.out.println("方法三,在Action中取得HttpServletRequest对象,使用request.getParameter获取参数:");     
  52. 52.        System.out.println("bookName: " +bookName);     
  53. 53.        System.out.println("bookPrice: " +bookPrice);     
  54. 54.        return SUCCESS;     
  55. 55.             
  56. 56.    }     
  57. 57.    
  58. 58.}    
 转自:http://chenpenghui.iteye.com/blog/1401499
原创粉丝点击