Java 在托管bean中传递表单值
我想将表单值传递到托管bean中,以便对其进行处理,但在尝试检索action方法中的值时,总是得到一个空值 我的豆子Java 在托管bean中传递表单值,java,jsf,managed-bean,Java,Jsf,Managed Bean,我想将表单值传递到托管bean中,以便对其进行处理,但在尝试检索action方法中的值时,总是得到一个空值 我的豆子 @ManagedBean(name="datas") @SessionScoped public class Datas implements Serializable { private static final long serialVersionUID = 1L; private String ID_primary; public Datas(
@ManagedBean(name="datas")
@SessionScoped
public class Datas implements Serializable {
private static final long serialVersionUID = 1L;
private String ID_primary;
public Datas(){
}
public Datas (String ID_primary){
this.ID_primary=ID_primary;
}
public String getID_primary() {
return ID_primary;
}
public void setID_primary(String ID_primary) {
this.ID_primary= ID_primary;
}
public void process() {
Map<String, String> request=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String value = request.get("ID_primary"); // always return null
System.out.println("ID" + value);
}
}
@ManagedBean(name=“datas”)
@会议范围
公共类数据实现可序列化{
私有静态最终长serialVersionUID=1L;
私有字符串ID_primary;
公共数据(){
}
公共数据(字符串ID_primary){
this.ID\u primary=ID\u primary;
}
公共字符串getID_primary(){
返回ID_primary;
}
public void setID_primary(字符串ID_primary){
this.ID\u primary=ID\u primary;
}
公共程序(){
映射请求=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String value=request.get(“ID_primary”);//始终返回null
系统输出打印项次(“ID”+值);
}
}
我的表格
<h:form>
<div align="left">
<h:inputText id="ID_primary" name="ID_primary" value="#{datas.ID_primary}" />
</div>
..........
<div align="right">
<h:commandButton action="#{datas.process()}" value="Create" type="submit" />
</div>
</h:form>
..........
非常感谢您的帮助您有两个不同的
表单
元素,当您单击底部表单
中的命令按钮
时,顶部表单
中的值不会随请求一起发送
您可以在表单
中包含类似div
的标记,因此,如果这是您拆分它们的原因,您可以在表单
中包含div
的大标签
还将@ManagedProperty
注释添加到ID\u主
字段:
public class Datas implements Serializable {
private static final long serialVersionUID = 1L;
@ManagedProperty(value = "#{Datas.ID_primary}")
private String ID_primary;
[..]
编辑:
好吧,我的错,我没有仔细看代码。在ManagedBean
withManagedProperty
中,此属性将由JSF自动设置,因此您可以这样阅读:
public void process() {
System.out.println("ID" + getID_primary());
}
非常感谢你。我更改了表单,但仍然得到空值:(。非常感谢,但我仍然有空值。当我进入调试模式时,我看到我的外部上下文中没有任何内容,所以问题可能就在这里。非常感谢。你救了我的命,我的朋友:)。它现在在managedProperty和getter中运行良好。我被外部环境的方式所困扰,但这种方式不起作用。用你的方式容易多了。再次感谢你,让我度过了美好的一天!谢谢