当前位置: 代码迷 >> java >> 如何在Java Struts2的模型中接收所有请求参数?
  详细解决方案

如何在Java Struts2的模型中接收所有请求参数?

热度:23   发布时间:2023-07-17 20:27:44.0

我正在尝试从具有modelobj的控制器中的表单接收所有请求参数。 但是模型参数仍然为空。 我不想在我的控制器类中创建模型的所有参数及其设置器/获取器...请帮助

形式:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<div>
    <form action="login" method="post">
    <label>Email Id</label>
    <input type="text" name="emailId">
    <label>Password</label>
    <input type="password" name="password">
    <input type="submit" value="Login">
    </form>
</div>
</body>
</html>

型号:

public class UserModel {
    String emailId;
    String password;
    public String getEmailId() {
        return emailId;
    }
    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

控制器:

import com.opensymphony.xwork2.ActionSupport;

public class LoginController extends ActionSupport{
    UserModel userModelObj = new UserModel();
    public UserModel getUserModelObj() {
        return userModelObj;
    }
    public void setUserModelObj(UserModel userModelObj) {
        this.userModelObj = userModelObj;
    }
    public String login()
    {
        UserModel userModelObj = new UserModel();
        System.out.println(userModelObj.emailId);
        System.out.println(userModelObj.password);
        return "success";
    }

}

在控制台上,它返回null null

您必须像这样在表单的name属性中使用bean类引用:

<div>
    <form action="login" method="post">
        <label>Email Id</label>
        <input type="text" name="userModelObj.emailId"/>
        <label>Password</label>
        <input type="password" name="userModelObj.password"/>
        <input type="submit" value="Login">
    </form>
</div>

否则,请使用ModelDriven接口的概念。

  相关解决方案