0

I have a problem about <h:inputText> of JSF, I'm trying to set value of a variable in a bean class but it can't set value by <h:inputText> tag. I'm a newbie about JSF.

My jsp code:

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="../style.css" />
<title>Tìm hồ sơ</title>
</head>
<body>
    <div id="content">
        <jsp:include page="header.jsp" />
        <h1>Nhập hồ sơ thí sinh - Kì tuyển sinh 2015</h1>
        <f:view>
                <h:form>
                    <font color="black"> Tìm hồ sơ: <br /></font>
                    <table>
                        <tr>
                            <td>SBD:</td>
                            <td><h:inputText id="sbd" value="#{hsinfo.fsbd}"
                                    required="true"></h:inputText></td>
                            <td><h:message for="sbd" /></td>
                        </tr>
                        <tr>
                            <td>Họ tên:</td>
                            <td><h:inputText id="ten" value="#{hsinfo.fname}"></h:inputText></td>
                        </tr>
                        <tr>
                            <td>Giới tính: <h:selectOneRadio id="gioitinh"
                                    value="#{hsinfo.fgioitinh}">
                                    <f:selectItem itemValue="M" itemLabel="Nam" />
                                    <f:selectItem itemValue="F" itemLabel="Nữ" />
                                </h:selectOneRadio>
                            </td>
                        </tr>
                        <div style="position: absolute;margin-top: 24px;margin-left: 654px;">
                            <h:commandButton
                                style="width:84px; height:84px;margin-top:-24px;"
                                action="#{hsinfo.findHS}" value="Tìm kiếm" immediate="true" />
                        </div>
                    </table>
                </h:form>
        </f:view>
    </div>
</body>
</html>

my bean class:

package Beans;

public class hsinfo {

    private String fsbd;
    private String fname;
    private String fgioitinh;



    public String getFsbd() {
        return fsbd;
    }

    public void setFsbd(String fsbd) {
        this.fsbd = fsbd;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getFgioitinh() {
        return fgioitinh;
    }

    public void setFgioitinh(String fgioitinh) {
        this.fgioitinh = fgioitinh;
    }

}

I made another jsp and bean class that successful to set value by <h:inputText> tag, but I don't know why it not work in this jsp and bean class!

halfer
  • 18,701
  • 13
  • 79
  • 158
  • Add the ManagedBean and proper scope annotation to your bean. Also, your web.xml needs to be configured to send that page to JSF – user489041 Sep 21 '15 at 17:06
  • 1
    If you're to JSF, then you should follow a simple JSF tutorial step-by-step. Then if you have any trouble following the tutorial, ask a short, specific question on StackOverflow. – DavidS Sep 21 '15 at 17:12
  • 1
    Also, JSF doesn't use JSP pages; it uses Facelets pages. Also, I'm surprised your `%@ taglib` imports are working: I've never seen that notation used, but maybe it's a JSP thing. Again, follow a recent tutorial, step by step. Don't just throw things together and hope it works. – DavidS Sep 21 '15 at 17:16
  • 2
    @DavidS: JSP was the default view technology in JSF 1.x. This thus suggests that OP is using JSF 1.x or was using completely outdated resources to learn JSF 2.x. The fact that OP has placed `[jsf-2]` tag on the question while showing a JSF 1.x compatible code snippet is indeed not a good sign. – BalusC Sep 21 '15 at 17:40

2 Answers2

4

Your problem is caused by having immediate="true" on the <h:commandButton>.

<h:commandButton ... action="#{bean.action}" immediate="true" />

It will skip processing of all input fields which doesn't have immediate="true".

Just get rid of it.

<h:commandButton ... action="#{bean.action}" />

See also:


Unrelated to the concrete problem, make sure you're using up to date resources to learn JSF. Your code is JSF 1.x style while we're already on JSF 2.x since 2009. Start here: https://stackoverflow.com/tags/jsf/info.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • what is the purpose of immediate in f:ajax ? In my testing it didn't skip validation. – Ced Sep 22 '15 at 01:03
0

You need :

  • @Named or @ManagedBean annotation on class

  • Scope

Geinmachi
  • 1,231
  • 1
  • 8
  • 20