• Struts2--属性设置方式


    Struts2自动获取/设置数据的方式一共分为两种

    • 属性驱动(FieldDriven)
    • 模型驱动(ModelDriven)
    1.  属性驱动

          属性又分为两种:

            |- 基本数据类型

            |- JavaBean属性类型

    基本数据类型:实例

     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     3 <html>
     4 <head>
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8     <form action="hello" method="get">
     9         姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
    10         年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>
    11         <button type="submit">提交</button>
    12     </form>
    13 </body>
    14 </html>
    package com.fuwh.struts;
    
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ModelDriven;
    
    public class HelloAction implements Action{
    
        private String name;
        private int age;
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            System.out.println("执行了HelloAction的默认方法");
            return SUCCESS;
        }
    
    }
     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     3 <html>
     4 <head>
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8 欢迎${age }岁的${name }光临
     9 </body>
    10 </html>
     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 
     6 <struts>
     7     <package name="mypack" extends="struts-default">
     8         <action name="hello" class="com.fuwh.struts.HelloAction">
     9             <result name="success">hello.jsp</result>
    10         </action>
    11     </package>
    12 </struts>

    JavaBean属性类型

     1 package com.fuwh.struts;
     2 
     3 public class User implements java.io.Serializable{
     4 
     5     private static final long serialVersionUID = 1L;
     6     
     7     private String name;
     8     private int age;
     9     
    10     public String getName() {
    11         return name;
    12     }
    13     public void setName(String name) {
    14         this.name = name;
    15     }
    16     public int getAge() {
    17         return age;
    18     }
    19     public void setAge(int age) {
    20         this.age = age;
    21     }
    22     
    23 }
     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     3 <html>
     4 <head>
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8     <form action="hello" method="get">
     9         姓名;<input type="text" name="user.name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
    10         年龄:<input type="text" name="user.age" size="10px" value="23" readonly="readonly"/>
    11         <button type="submit">提交</button>
    12     </form>
    13 </body>
    14 </html>
     1 package com.fuwh.struts;
     2 
     3 import com.opensymphony.xwork2.Action;
     4 
     5  public class HelloAction implements Action{ 
     6 
     7    private User user 
     8 
     9    public User getUser() {
    10         return user;
    11     }
    12     public void setUser(User) {
    13         this.user = user;
    14     }
    15     @Override
    16     public String execute() throws Exception {
    17         // TODO Auto-generated method stub
    18         System.out.println("执行了HelloAction的默认方法");
    19         return SUCCESS;
    20     }
    21 
    22 }
     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     3 <html>
     4 <head>
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8 欢迎${user.age }岁的${user.name }光临
     9 </body>
    10 </html>

    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 
     6 <struts>
     7     <package name="mypack" extends="struts-default">
     8         <action name="hello" class="com.fuwh.struts.HelloAction">
     9             <result name="success">hello.jsp</result>
    10         </action>
    11     </package>
    12 </struts>

    模型驱动

     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     3 <html>
     4 <head>
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8     <form action="hello" method="get">
     9         姓名;<input type="text" name="name" size="10px" value="fuwh" readonly="readonly"/>&nbsp;
    10         年龄:<input type="text" name="age" size="10px" value="23" readonly="readonly"/>
    11         <button type="submit">提交</button>
    12     </form>
    13 </body>
    14 </html>
     1 package com.fuwh.struts;
     2 
     3 import com.opensymphony.xwork2.Action;
     4 import com.opensymphony.xwork2.ModelDriven;
     5 
     6 public class HelloAction implements Action,ModelDriven{
     7 
     8     private User user=new User();
     9     
    10     @Override
    11     public String execute() throws Exception {
    12         // TODO Auto-generated method stub
    13         System.out.println("执行了HelloAction的默认方法mae");
    14         return SUCCESS;
    15     }
    16     @Override
    17     public User getModel() {
    18         // TODO Auto-generated method stub
    19         System.out.println("执行了getModel方法");
    20         return this.user;
    21     }
    22 }
     1 <%@ page language="java" pageEncoding="UTF-8"%>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     3 <html>
     4 <head>
     5 <title>Insert title here</title>
     6 </head>
     7 <body>
     8 欢迎${age }岁的${name }光临
     9 </body>
    10 </html>
     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 
     6 <struts>
     7     <package name="mypack" extends="struts-default">
     8         <action name="hello" class="com.fuwh.struts.HelloAction">
     9             <result name="success">hello.jsp</result>
    10         </action>
    11     </package>
    12 </struts>

    Struts处理多值参数

     普通类型

     普通类型和负责类型一样,可以选择用集合来接收,也可以用数组来接收

    复杂类型

    package com.fuwh.demo;
    
    public class User {
    
        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;
        }
        @Override
        public String toString() {
            return "User [age=" + age + ", name=" + name + "]";
        }
        
        
    }
    View Code
    <%@ page language="java" pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
    <form action="show" method="post">
        兴趣爱好:<input type="text" name="stu[0].age" value="5">
                <input type="text" name="stu[0].name" value="fuwh1"><br/>
                <input type="text" name="stu[1].age" value="6">
                <input type="text" name="stu[1].name" value="fuwh2"><br/>
                <input type="text" name="stu[2].age" value="7">
                <input type="text" name="stu[2].name" value="fuwh3"><br/>
        <input type="submit">
    </form>
    </body>
    </html>
    package com.fuwh.demo;
    
    
    import java.util.List;
    
    import com.opensymphony.xwork2.Action;
    
    public class Show implements Action{
    
        private List<User> stu;
        
        public List<User> getStu() {
            System.out.println("execut the get method!");
            return stu;
        }
    
        public void setStu(List<User> stu) {
            System.out.println("execute the set method!");
            this.stu = stu;
        }
    
        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            System.out.println("执行了action的默认方法");
            System.out.println(stu);
            return SUCCESS;
        }
    
        
    
        
        
    }
    View Code
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <!-- 开启debug模式,会自动加载配置文件等等,不用每次更改了配置文件就去重新启动下服务器 -->
        <constant name="struts.devMode" value="true" />
        
        <package name="test"  extends="struts-default">
            <action name="show" class="com.fuwh.demo.Show">
                <result name="success">hello.jsp</result>
            </action>
            
        </package>
    
    </struts>
    View Code
    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <head>
    <title>Insert title here</title>
    </head>
    <body>
    学生们:${stu}
    </body>
    </html>
    View Code

    最后出力结果:

  • 相关阅读:
    序列化注意事项
    HTML5的新结构标签
    MVC模型
    CSS选择器权重计算规则
    HTML常用布局
    盒模型
    Spring Security 学习笔记-session并发控制
    java实例之随机点名
    java之方法重载
    java之方法
  • 原文地址:https://www.cnblogs.com/zerotomax/p/5906578.html
Copyright © 2020-2023  润新知