简体   繁体   English

如何将表单的值从jsp发送到Java

[英]How to send values of a form from jsp to java

I have a form which has 30 different fields. 我有一个包含30个不同字段的表格。 As passing them all to the controller need to have an attribute for each along with a pair of getter, setters. 将它们全部传递给控制器​​时,需要为每个属性都具有一个属性,以及一对getter,setter。

I am going to make the form fields as an object and send the object to the controller. 我将使表单字段作为一个对象并将该对象发送到控制器。

I am using the following code * but some people suggest its a bad practice to call a java method from jsp and use JSTL instead, but do not know how to implement it using JSTL. 我正在使用以下代码*, 但有人建议从jsp调用java方法并改为使用JSTL是一种不好的做法,但不知道如何使用JSTL来实现它。 Is there any other method to do it? 还有其他方法吗? * *

My JSP 我的JSP

 <s:form>
 code to implement form goes here
 </s:form> 

<jsp:useBean id="obj" class="com.User"/>

    <jsp:setProperty property="*" name="obj"/>

      <%
         String myoutput = myController.Xclass(obj);
         out.print(myoutput);
         if(myController.Xclass(obj).equals("output"))
            {
               out.print("The form is successfully submitted.");
            }
      %>

The controller 控制器

  public String Xclass(User obj){
           return "output";
        }

To clarify my class diagram is a following: 以下是澄清我的类图的方法:

User Class {
 all the attributes and getters setters
}

myController class extends User {

    public String XClass(User obj){
       ... work on the inputes ...
      return "output";
    }
}

If the number of action properties is the issue, expose a POJO manually, or use ModelDriven . 如果问题是动作属性的数量,请手动公开POJO或使用ModelDriven

Doing it manually is simple, for example: 手动执行很简单,例如:

public class UserController {
    private User user; // Plus public getter and setter
}

Then in the JSP you can refer to User properties by name: 然后,在JSP中,您可以按名称引用User属性:

<s:form ...>
  <s:textfield key="user.firstName"/>
  ...

Using ModelDriven is theoretically even easier, since it's put on the stack automagically. ModelDriven理论上讲,使用ModelDriven更容易,因为它可以自动放置在堆栈中。 It can be tricky to make sure new models are instantiated only when required, but basically (from memory): 确保仅在需要时才实例化新模型,但基本上(从内存中)实例化是很棘手的:

public class UserController implements ModelDriven<User> {
    private User user;
    public User getModel() { return user; }
}

Use the User properties directly in the JSP since the User is pushed on the stack: 由于将User压入堆栈,因此直接在JSP中使用User属性:

<s:form ...>
  <s:textfield key="firstName"/>
  ...

Similarly, on form submission, a model is created and used as the first target of methods. 同样,在提交表单时,会创建一个模型并将其用作方法的第一个目标。

Please remember that you never send objects to the Java side: you always, and only, send strings (from normal HTTP form submissions). 请记住,您永远不会将对象发送到Java端:您总是并且仅发送字符串(通过常规HTTP表单提交)。 There may be magic on the server side that transforms those strings into objects, but it's just that: magic. 服务器端可能会有魔术将这些字符串转换为对象,但这仅仅是魔术。 Magic and hope. 魔术和希望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM