Spring表单:选择不在表单提交上调用转换器

时间:2017-11-26 20:42:23

标签: java spring forms select converter

我有一个非常奇怪且相当不一致的问题,我不确定是什么原因引起的。我正在将我的表格中的所有下拉菜单从非Spring选择更改为表单:选择,因为我发现它们更清洁,更简单。这是我的旧方法,它正确调用了strengthUnit转换器,因此为我的控制器提供了一个非null对象:

dim = 2             #Defines the dimensionality of the system
n = 25               #Number of BOIDS
tmax = 80             #Length of sim
dmax = 5            #Distance boids can "see", determines what other boids interact with them
o = np.zeros(dim) #Origin as vector
r = np.random.rand(n,dim) #Places BOIDs randomly with co-ordinates (x,y,z) from 0 to 1. Has dimensions n and dim
v = 2*np.random.rand(n,dim)-1#Sets initial velocity of each BOID from -1 to 1 in each cardinal direction
rt = np.zeros((tmax,n,dim)) #This array contains the whole system's positions at each point in time
x = np.empty(n)
y = np.empty(n)
d = np.zeros(n)
vk = np.zeros((n,2))
vksum = np.zeros((n,2))
pltx = np.zeros((tmax,n))
plty = np.zeros((tmax,n))
"""rt[a][b][0] is the x co-ordinate of boid n=b at t=a
   rt[a][b][1] is the y co-ordiante of boid n=b at t=a
   np.linalg.norm gives the modulus of an array, check documentation for arguments"""

fig, ax = plt.subplots(figsize=(14,9))
ax.grid(True,linestyle='-',color='0.75') #Sets up a grid on subplot
ax.set_xlim(-50,50)
ax.set_ylim(-50,50) #Set limits for x and y axes

# initialize an empty PathCollection artist, to be updated at each iteration
points = ax.scatter([],[],c='r')    

for t in range (0,tmax):
    for i in range (0,n):
        for k in range (0,n):
            if abs(k-n)>0:
                d[k] = ((r[i][0]-r[k][0])**2+(r[i][1]-r[k][1])**2)**(1/2) #Checks distance from ith boid to each other boid
            if (d[k]-dmax)<0:   #If they are within range of the ith boid
                vk[k] = (v[i] +v[k])/((np.linalg.norm(v[i]))*np.linalg.norm(v[k]))#Aligns the velocity of ith boid toward the velocity of the kth boid
        for l in range (0,n):
            vksum[i] = vksum[i] + vk[l] #Sums the boid's velocity contributions together
        v[i] = (3/4)*v[i] + (vksum[i]/np.linalg.norm(vksum[i])) #Sets the boid's new velocity 
        r[i] = r[i] + v[i]  #Sets the boid's new position
        rt[t][i] = r[i] #Logs the position of the boid in the time array
        pltx[t][i] = r[i][0]
        plty[t][i] = r[i][1]


def init():
    for i in range (0,n):
        x[i] = rt[0][i][0]
        y[i] = rt[0][i][1]
    return x,y,  

def update(j):
    for i in range (0,n):
        x[i] = rt[j][i][0]
        y[i] = rt[j][i][1]
    xy = np.hstack((x,y))
    points.set_offsets(xy) # update the coordinates of the PathCollection members
    return points, # return the updated artist(s) for blitting

anim = animation.FuncAnimation(fig, update, frames=tmax, interval=50,blit=True)

我已将此代码更改为下面的代码,现在转换器未被调用,当对象触及控制器时该对象为空:

def mypow(a,b):
    if b == 0:
        return 1
    if b == 1:
        return a
    elif b > 1:
        x = 0 
        for i in range(b):
            x += 1 * a
        return x 
    # I know I got to add what happens if the b is negative, but I will do this after fixing the bug.

哪里更奇怪的是,这个特定的实体类型附加到2个不同的实体和形式:select标签在另一个页面中工作!两个JSP都有这一行:

<select name="strengthUnit" path="strengthUnit.name" id="strengthUnit">
                        <option value="0" ${orderedMed.strengthUnit eq null ? 'selected' : ''}></option>
                        <c:forEach items="${strengthUnits}" var="strengthUnitSingle">
                            <option value="${strengthUnitSingle.id}" ${orderedMed.strengthUnit.name eq strengthUnitSingle.name ? 'selected' : ''}>${strengthUnitSingle.name}</option>
                        </c:forEach>
                    </select>

这是strengthUnitConverter类的convert方法,它在config类中正确注册并使用第一种方法工作:

<form:select name="strengthUnit" path="strengthUnit.id" id="strengthUnit">
                        <form:option value="0" label=""></form:option>
                        <form:options items="${strengthUnits}" itemLabel="name" itemValue="id"/>
                    </form:select>

谢谢!

1 个答案:

答案 0 :(得分:0)

发现问题! path属性需要指向实体本身而不是其ID:

<form:select name="strengthUnit" path="strengthUnit" id="strengthUnit">
                    <form:option value="0" label=""></form:option>
                    <form:options items="${strengthUnits}" itemLabel="name" itemValue="id"/>
                </form:select>

我认为这是在我的另一页工作,但发现今天早上我错了。修正路径后,我不再获得空对象。