7.Struts2复杂类型数据的接受

复合类型数据的接收
所谓复合类型数据是指,一个JavaBean实例的属性值,而这些值又作为参数传递给Action。
Action若要接收这些数据,需要做到以下几点:
(1)定义Action时,将该Bean的实例定义为该Action的属性,并赋予其get与set方法。
(2)参数的形式为:bean实例.属性
(3)若复合型数据还需要在视图中显示,如在EL表达式中出现,其出现形式也为:bean实例.属性

源码文档建立目录如下:

7.Struts2复杂类型数据的接受_第1张图片

Complex_Params_Action.java源码如下:

package actions;

import dto.UserDto;

public class Complex_Params_Action {
    private UserDto user;

    public UserDto getUser() {
        return user;
    }

    public void setUser(UserDto user) {
        this.user = user;
    }
    
    public String execute(){
        System.out.println("user:"+user);
        return "success";
    }    

}

UserDto.java源码如下:

package dto;

public class UserDto {
    private String username;
    private int age;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    public String toString(){
        return "username="+username+",age="+age;
    }

}

index.jsp源码如下:

<%@ page  pageEncoding="UTF-8"%>
<html>
  <head>
    
    <title>注册页面</title>

  </head>
  
  <body>
           <form action="complex.action" method="post">
           用户名<input type="text" name="user.username"/><br/>
           年龄<input type="text" name="user.age"/><br/>
           <input type="submit" value="注册"/>
           </form>
  </body>
</html>

welcome.jsp源码如下:

<%@ page pageEncoding="utf-8" isELIgnored="false"%>

<html>
  <head>

    
    <title>welcome page</title>


  </head>
  
  <body>
        用户名:${user.username}<br/>
        年龄:${user.age}
   
  </body>
</html>

web.xml配置文档如下:

<?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">
    
        <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>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</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>

    <package name="one" extends="struts-default">
    
        <action name="complex" class="actions.Complex_Params_Action">
            <result>/welcome.jsp</result>
        </action>
        
    </package>
</struts>

重新部署发布,启动tomcata,地址输入:

http://127.0.0.1:8080/receive_complex_params/

7.Struts2复杂类型数据的接受_第2张图片

7.Struts2复杂类型数据的接受_第3张图片

 

你可能感兴趣的:(struts2)