今天配置struts hibernate环境时遇到一个问题。 这是我的资源实体中的一个属性,get set方法使用eclipse generate 方法生成的。
1
2
3
4
5
6
7
8
9
10
private int pId;
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">getpId</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">pId</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setpId</span><span class="o">(</span><span class="kt">int</span> <span class="n">pId</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">pId</span> <span class="o">=</span> <span class="n">pId</span><span class="o">;</span>
<span class="o">}</span>
<span class="w">
可这样在表单中:
1
<input type="text" name="resource.pId">
总是在action中取不到pId的值,同时在后台保存一个new的resource是成功的。 后来才发现struts和hibernate对get set方法的要求存在一点不同。 eclipse的自动生成的get set 方法,hibernate是认可的。 struts则需要注意一点,就是类似于pId这样的命名,struts认可的set get方法是:
1
2
3
4
5
6
public void setPId(int pId) {
this.pId = pId;
}
public int getPId() {
return pId;
}
所以最后的解决办法就是
1. 不嫌麻烦的可以写成这样
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private int pId;
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">getpId</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">pId</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setpId</span><span class="o">(</span><span class="kt">int</span> <span class="n">pId</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">pId</span> <span class="o">=</span> <span class="n">pId</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setPId</span><span class="o">(</span><span class="kt">int</span> <span class="n">pId</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">pId</span> <span class="o">=</span> <span class="n">pId</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">getPId</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">pId</span><span class="o">;</span>
<span class="o">}</span><span class="w">