如何将表单的值从jsp发送到Java [英] How to send values of a form from jsp to java

查看:85
本文介绍了如何将表单的值从jsp发送到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含30个不同字段的表格.将它们全部传递给控制器​​时,需要为每个属性都具有一个属性,以及一对getter,setter.

I have a form which has 30 different fields. As passing them all to the controller need to have an attribute for each along with a pair of getter, setters.

我将把表单字段作为对象,并将该对象发送给控制器.

I am going to make the form fields as an object and send the object to the controller.

我正在使用以下代码 * ,但是有人建议从jsp调用Java方法并改为使用JSTL是一种不好的做法,但不知道如何使用JSTL来实现它. 还有其他方法吗? *

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. Is there any other method to do it?*

我的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.");
            }
      %>

控制器

  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";
    }
}

推荐答案

如果存在许多操作属性,请手动公开POJO或使用

If the number of action properties is the issue, expose a POJO manually, or use ModelDriven.

手动完成很简单,例如:

Doing it manually is simple, for example:

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

然后在JSP中,您可以按名称引用User属性:

Then in the JSP you can refer to User properties by name:

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

从理论上讲,使用ModelDriven更加容易,因为它会自动放置在堆栈中.确保仅在需要时实例化新模型可能很棘手,但是基本上(从内存中)实例化

Using ModelDriven is theoretically even easier, since it's put on the stack automagically. 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; }
}

由于User被压入堆栈,因此直接在JSP中使用User属性:

Use the User properties directly in the JSP since the User is pushed on the stack:

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

类似地,在表单提交时,会创建一个模型并将其用作方法的第一个目标.

Similarly, on form submission, a model is created and used as the first target of methods.

请记住,您永远不会将对象发送到Java端:您始终(唯一)发送字符串(通过常规HTTP表单提交).服务器端可能会有魔术将这些字符串转换为对象,但这仅仅是魔术.魔术和希望.

Please remember that you never send objects to the Java side: you always, and only, send strings (from normal HTTP form submissions). There may be magic on the server side that transforms those strings into objects, but it's just that: magic. Magic and hope.

这篇关于如何将表单的值从jsp发送到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆