zoukankan      html  css  js  c++  java
  • Struts2_属性驱动

    在jsp页面提交到action中的表单元素在action中是以对象的形式存在的,

    action中的对象name必须与jsp页面中的表单元素name相同,struts框架自动为action的对象赋值.

    一.普通驱动

    注意:action中的对象name必须与jsp页面中的表单元素name相同,struts框架自动为action的对象赋值.

    案例:

    struts.xml文件:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <!DOCTYPE struts PUBLIC
     4     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     5     "http://struts.apache.org/dtds/struts-2.3.dtd">
     6 
     7 <struts>
     8     <package name="Hello" extends="default-struts" namespace="">
     9         <action name="Hello" class="com.ahd.action.HelloAction">
    10             <result>/success.jsp</result>
    11         </action>
    12     </package>
    13 </struts>

    jsp页面:

    1 <body>
    2     <form action="hello.action">
    3         用户名:<input type="text" name="username"/><br/>
    4         密    码:<input type="password" name="password"/><br/>
    5         <input type="submit" value="submit"/>
    6         <input type="reset" name="reset"/><br/>
    7     </form>
    8     
    9   </body>

    action页面:

     1 package com.ahd.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class HelloAction extends ActionSupport{
     6     private String username;
     7     private String password;
     8     
     9     @Override
    10     public String execute() throws Exception {
    11         // TODO Auto-generated method stub
    12         return super.execute();
    13     }
    14 
    15     public String getUsername() {
    16         return username;
    17     }
    18 
    19     public void setUsername(String username) {
    20         this.username = username;
    21     }
    22 
    23     public String getPassword() {
    24         return password;
    25     }
    26 
    27     public void setPassword(String password) {
    28         this.password = password;
    29     }
    30     
    31 }

    二.属性驱动

    普通驱动如果是大项目表单提供过多元素action会比较乱,属性驱动就是将表单提供的元素封装到一个对象中,这个对象作为action中的属性,

    在前端页面表单中需要提交的元素name统一写成 对象.name

    案例:

    实体类User

     1 package com.ahd.entity;
     2 
     3 public class User {
     4     private String username;
     5     private String password;
     6     public String getUsername() {
     7         return username;
     8     }
     9     public void setUsername(String username) {
    10         this.username = username;
    11     }
    12     public String getPassword() {
    13         return password;
    14     }
    15     public void setPassword(String password) {
    16         this.password = password;
    17     }
    18 }

    HelloAction:

     1 package com.ahd.action;
     2 
     3 import com.ahd.entity.User;
     4 import com.opensymphony.xwork2.ActionSupport;
     5 
     6 public class HelloAction extends ActionSupport{
     7     private User user;
     8     
     9     @Override
    10     public String execute() throws Exception {
    11         // TODO Auto-generated method stub
    12         System.out.println(user.getUsername()+user.getPassword());
    13         return super.execute();
    14     }
    15 
    16     public User getUser() {
    17         return user;
    18     }
    19 
    20     public void setUser(User user) {
    21         this.user = user;
    22     }
    23     
    24 }

    index.jsp:

    1   <body>
    2     <form action="hello.action">
    3         用户名:<input type="text" name="user.username"/><br/>
    4         密    码:<input type="password" name="user.password"/><br/>
    5         <input type="submit" value="submit"/>
    6         <input type="reset" name="reset"/><br/>
    7     </form>
    8     
    9   </body>

    strust.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 
     3 <!DOCTYPE struts PUBLIC
     4     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     5     "http://struts.apache.org/dtds/struts-2.3.dtd">
     6 
     7 <struts>
     8     <package name="Hello" extends="default-struts" namespace="">
     9         <action name="Hello" class="com.ahd.action.HelloAction">
    10             <result>/success.jsp</result>
    11         </action>
    12     </package>
    13 </struts>
  • 相关阅读:
    线程的生命周期
    同步与死锁
    同步与死锁
    线程的休眠
    线程的休眠
    RTSP转RTMP、FLV、HLS网页无插件视频直播-LiveNVR功能介绍-音频开启
    使用LiveNVR实现将RTSP转RTMP、FLV、HLS,实现监控摄像头无插件直播
    使用LiveNVR实现安防摄像头RTSP流WEB无插件播放的延迟调优
    Javascript之数据执行原理探究
    Javascript之动画1
  • 原文地址:https://www.cnblogs.com/aihuadung/p/9912995.html
Copyright © 2011-2022 走看看