在Struts 2中提交后如何从表单中输入的数据更新模型 [英] How to update the model from the data entered in the form after submit in Struts 2

查看:78
本文介绍了在Struts 2中提交后如何从表单中输入的数据更新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Struts2中有一个具有不同类型字段的表单.当我在表单中输入数据并提交时,没有输入的数据会被赋值,并且在ActionSupport类中它们是null.下面是我的问题样本

I have a form in Struts2 with different types of fields. When I enter data into the form and submit it, no data entered are valued and in the ActionSupport class they are null. Below a sample of my problem

struts.xml:

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>
    <constant name="struts.devMode" value="false" />
    <constant name="struts.action.extension" value="do" />
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.objectFactory.spring.autoWire" value="name" />
    <constant name="struts.i18n.encoding" value="ISO-8859-1" />
    <constant name="struts.i18n.reload" value="false" />
    <constant name="struts.configuration.xml.reload" value="false" />
    <constant name="struts.locale" value="fr" />
    <constant name="struts.multipart.maxSize" value="100000000000" />

    <package name="default" extends="tiles-default" namespace="/">

        <interceptors>

            <interceptor name="params-filter"
                class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />
            <interceptor-stack name="defaultStack">
                <interceptor-ref name="exception" />
                <interceptor-ref name="alias" />
                <interceptor-ref name="servlet-config" />
                <interceptor-ref name="i18n" />
                <interceptor-ref name="chain" />
                <interceptor-ref name="model-driven" />
                <interceptor-ref name="fileUpload">
                    <param name="maximumSize">11204928</param>
                </interceptor-ref>
                <interceptor-ref name="static-params" />
                <interceptor-ref name="conversionError" />
                <interceptor-ref name="params" />
                <interceptor-ref name="prepare" />
                <interceptor-ref name="validation" />
                <interceptor-ref name="workflow" />
                <interceptor-ref name="userAware" />
            </interceptor-stack>

        </interceptors>

        <default-interceptor-ref name="defaultStack" />

        <global-results>
            <result name="technicalError" type="chain">
                errorAction
            </result>
            <result name="sessionInvalidError" type="tiles">
                sessionInvalid
            </result>
            <result name="blank" type="tiles">blank</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="technicalError" />
            <exception-mapping
                exception="com.omb.service.exception.UserSessionInvalidException"
                result="sessionInvalidError" />

        </global-exception-mappings>

        <package name="omb" extends="default" namespace="/omb">
            <action name="*OMBAction" class="com.omb.MyAction" method="doMyAction">
            <result name="ok" type="tiles">maPage</result>
        </action>
    </package>
</struts>

JSP文件:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display"%>
<link rel="stylesheet" href="<s:url value="/windowfiles/dhtmlwindow.css"/>" type="text/css" />



<script language="javascript"><!--
mes scripts
--></script>
<s:form action="valoriseHiddenOMBAction.do" theme="simple" id="hiddenForm">
    <s:hidden name="champ1" value="%{champ1}"></s:hidden>
    <s:hidden name="champ2" value="%{champ2}"></s:hidden>
</s:form>

<table height="100%" width="100%" border="0">
    <s:form action="doOMBAction.do" name="ombForm" id="idOmbForm" theme="simple">

        <s:hidden name="user.userId" value="%{user.userId}"></s:hidden>
        <s:hidden name="period.periodId" value="%{period.periodId}"></s:hidden>

        <tr>
            <td>
                <input  type="button"  value="download" onclick = "javascript:location.href='afficheOMBReport.do';"/>
            </td>
        </tr>
        <tr>
            <td width="10%"><label ><s:text name="champ1.label"></s:text></label></td>
            <td width="4%">&nbsp;</td>
            <td width="10%"><label ><s:text name="champ2.label"></s:text></label></td>
        </tr>
        <tr>
            <td>
                <s:select cssStyle="width:200" list="listChamp1" id="idChamp1"
                    value="champ1.identifiant" listKey="identifiant" listValue="label"/>
            </td>
            <td>&nbsp;</td>
            <td>
                <s:select cssStyle="width:200" list="listChamp2" id="idChamp2"
                    value="champ2.identifiant" listKey="identifiant" listValue="label"/>
            </td>
        </tr>
        <tr>
            <td>
                <input  type="button"  value="Afficher les valeurs" onclick = "javascript:location.href='afficheValuesOMBAction.do';"/>
            </td>
        </tr>
    </s:form>
</table>

Java文件:

Champ.java:

Champ.java :

/**
 * 
 */
package com.omb;

public class Champ {

    private Integer identifiant;

    private String label;

    public Champ(Integer identifiant, String label) {
        super();
        this.identifiant = identifiant;
        this.label = label;
    }

    /**
     * @return the identifiant
     */
    public Integer getIdentifiant() {
        return identifiant;
    }

    /**
     * @param identifiant the identifiant to set
     */
    public void setIdentifiant(Integer identifiant) {
        this.identifiant = identifiant;
    }

    /**
     * @return the label
     */
    public String getLabel() {
        return label;
    }

    /**
     * @param label the label to set
     */
    public void setLabel(String label) {
        this.label = label;
    }

}

MyAction类:

The MyAction class:

/**
 * 
 */
package com.omb;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author ombinte
 * 
 */
public class MyAction extends ActionSupport {

    private Champ champ1;

    private Champ champ2;

    private List<Champ> listChamp1;

    private List<Champ> listChamp2;

    public String execute() throws Exception {

        listChamp1 = new ArrayList<Champ>();
        listChamp1.add(new Champ(1, "valeur1"));
        listChamp1.add(new Champ(2, "valeur2"));

        listChamp2 = new ArrayList<Champ>();
        listChamp2.add(new Champ(3, "valeur3"));
        listChamp2.add(new Champ(4, "valeur4"));

        return SUCCESS;
    }

    public String doMyAction() {

        System.out.println("ID Champ 1 = " + champ1.getIdentifiant());
        System.out.println("Label Champ 1 = " + champ1.getLabel());
        System.out.println("ID Champ 2 = " + champ2.getIdentifiant());
        System.out.println("Label Champ 2 = " + champ2.getLabel());

        return "ok";
    }

    /**
     * @return the champ1
     */
    public Champ getChamp1() {
        return champ1;
    }

    /**
     * @param champ1 the champ1 to set
     */
    public void setChamp1(Champ champ1) {
        this.champ1 = champ1;
    }

    /**
     * @return the champ2
     */
    public Champ getChamp2() {
        return champ2;
    }

    /**
     * @param champ2 the champ2 to set
     */
    public void setChamp2(Champ champ2) {
        this.champ2 = champ2;
    }

    /**
     * @return the listChamp1
     */
    public List<Champ> getListChamp1() {
        return listChamp1;
    }

    /**
     * @param listChamp1 the listChamp1 to set
     */
    public void setListChamp1(List<Champ> listChamp1) {
        this.listChamp1 = listChamp1;
    }

    /**
     * @return the listChamp2
     */
    public List<Champ> getListChamp2() {
        return listChamp2;
    }

    /**
     * @param listChamp2 the listChamp2 to set
     */
    public void setListChamp2(List<Champ> listChamp2) {
        this.listChamp2 = listChamp2;
    }

}

推荐答案

在您的情况下,该模型是一个动作类,因为您没有实现ModelDriven.提交表单时,应指定name属性,该属性包含操作类的bean属性名的值.使用OGNL通过params拦截器填充该bean,以访问该bean的属性.

The model is an action class in your case, because you don't implement ModelDriven. When you submit the form you should specify the name attribute which contain the value of the bean's property name of the action class. The bean is populated via the params interceptor using OGNL to access the bean properties.

在代码中,不会填充隐藏字段,因为操作Bean中没有此类属性.其他输入字段没有填充,因为它们没有name属性.要解决此问题,您应该创建属性和访问器,对其进行初始化,这样OGNL不会捕获尝试访问它们的NPE.在JSP中,将name属性添加到s:select标记.例如

In your code hidden fields aren't populated because you don't have such properties in the action bean. Other input fields aren't populated because they doesn't have name attribute. To fix the issue you should create properties and accessors, initialize them so, OGNL would not catch NPEs trying to access them. In the JSP add name attribute to s:select tag. For example

<s:select cssStyle="width:200" list="listChamp1" id="idChamp1"
  name="champ1.identifiant" listKey="identifiant" listValue="label"/>

这篇关于在Struts 2中提交后如何从表单中输入的数据更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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