Forwarding to same page with same managed bean - jsf

I am usnig jsf 2.1 and PrimeFaces 4.0.
I have simple html page index.html which is having a link as :
<a href="pages/findus.jsf">
this link open's a page findus.jsf, on this page I have command button as:
<p:commandButton value="Find" action="#{findUsBean.onClickBtnFindUs}">
in onClickBtnFindUs I need setup some values to fields of FindUsBean and redirect to same page means findus.jsf and want to retrieve that values on findus.jsf page.
following is my complete FindUsBean.
#ManagedBean(name = "findUsBean")
#RequestScoped
public class FindUsBean implements Serializable{
private static final long serialVersionUID = 1L;
#ManagedProperty(value = "#{findUsService}")
private FindUsService findUsService;
private String state;
private String city;
private String zip;
private String result;
public String onClickBtnFindUs(){
RestaurantLocationMaster restoLoc = new RestaurantLocationMaster();
System.out.println(getState());
restoLoc.setResState(getState());
restoLoc.setResCity(getCity());
restoLoc.setResZip(getZip());
restoLoc = findRestaurantLocation(restoLoc);
if(null != restoLoc){
setState(restoLoc.getResState());
setCity(restoLoc.getResCity());
setZip(restoLoc.getResZip());
setResult(restoLoc.getResAddress1()+","+restoLoc.getResAddress2());
}else{
setResult("Comming soon...");
}
System.out.println(getState() +""+getCity());
return "/pages/findus.jsf";
}
public RestaurantLocationMaster findRestaurantLocation(RestaurantLocationMaster restoLoc){
RestaurantLocationMaster restoLoc1 = null;
restoLoc1 = findUsService.findRestaurantLocation(restoLoc);
return restoLoc1;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public void setFindUsService(FindUsService findUsService) {
this.findUsService = findUsService;
}
public FindUsService getFindUsService() {
return findUsService;
}
public void setResult(String result) {
this.result = result;
}
public String getResult() {
return result;
}
}
and my findus.jsf is:
<h:form prependId="false" method='POST'>
<table>
<tr>
<td><p:outputLabel value="State"></p:outputLabel>
</td>
<td><p:inputText maxlength="20" value="#{findUsBean.state}"></p:inputText>
</td>
</tr>
<tr>
<td><p:outputLabel value="city"></p:outputLabel>
</td>
<td><p:inputText maxlength="20" value="#{findUsBean.city}"></p:inputText>
</td>
</tr>
<tr>
<td><p:outputLabel value="zip"></p:outputLabel>
</td>
<td><p:inputText maxlength="8" value="#{findUsBean.zip}"></p:inputText>
</td>
</tr>
<tr>
<td colspan="2" align="right"><p:commandButton value="Find"
action="#{findUsBean.onClickBtnFindUs}">
</p:commandButton>
</td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td></td>
<td><p:inputTextarea value="#{findUsBean.result}"
readonly="true"></p:inputTextarea>
</td>
</tr>
</table>
I am not able to get values. If this is wrong way then please suggest me proper one.

The problem ist the scope of your bean. You should use #ViewScope or a larger scope.
Have a look
here

Related

How to dynamically hide and show html <tr>

How to add code in OnInitializedAsync to show/hide the in C# razor pages?
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
<tr id="rowId" style="display: none;">
<td>
<label>test</label>
</td>
</tr>
</tbody>
</table>
</div>
#code {
protected override void OnInitializedAsync()
{
// code to show <tr> with id="rowId"
}
}
With the following code, when the component is initialized the table row is hidden. When the button is pressed it becomes visible.
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
#if (_rowVisible)
{
<tr id="rowId">
<td>
<label>test</label>
</td>
</tr>
}
</tbody>
</table>
</div>
<button #onclick="OnHideRowBtnClicked">Hide row</button>
#code {
private bool _rowVisible;
protected override void OnInitializedAsync()
{
// set _rowVisible to true if row should be visible or false if row should be hidden
_rowVisible = true;
}
private void OnHideRowBtnClicked()
{
_rowVisible = false;
}
}
Another approach using the style attribute:
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
<tr id="rowId" style="#_rowStyle">
<td>
<label>test</label>
</td>
</tr>
</tbody>
</table>
</div>
<button #onclick="OnHideRowBtnClicked">Hide row</button>
<button #onclick="OnShowRowBtnClicked">Show row</button>
#code {
private string? _rowStyle;
protected override void OnInitializedAsync()
{
}
private void OnHideRowBtnClicked()
{
_rowStyle = "display: none;";
}
private void OnShowRowBtnClicked()
{
_rowStyle = null;
}
}

Unable to call method specified in ValueChangeListener attribute of <ice:selectOneRadio>

I am creating a Icefaces Datatable.First column have radio button. On clicking of radio button of first row i am able to edit the row. On clicking of radio button of another rows another operation should be performed(but not edit). I have one command button at the bottom of the datatable which saves the data. Find below the code for the same
------------------xhtml----------------------
<h1>IceFaces 3 </h1>
<h:form>
<ice:selectOneRadio id="myRadioId"
value="#{order.checked}" layout="spread"
valueChangeListener="#{orderBean.editSelectedRow}"
partialSubmit="true">
<f:selectItem itemValue="" />
</ice:selectOneRadio>
<ace:dataTable value="#{orderBean.orderList}" var="o" style="width: 300px !important">
<ace:column headerText="Select">
<ice:radio for="myRadioId" />
</ace:column>
<ace:column headerText="Order No">
<h:inputText value="#{o.orderNo}" size="10" rendered="#{o.editable}" />
<h:outputText value="#{o.orderNo}" rendered="#{not o.editable}" />
</ace:column>
<ace:column headerText="Product Name">
<h:inputText value="#{o.productName}" size="20" rendered="#{o.editable}" />
<h:outputText value="#{o.productName}" rendered="#{not o.editable}" />
</ace:column>
<ace:column headerText="Price">
<h:inputText value="#{o.price}" size="10" rendered="#{o.editable}" />
<h:outputText value="#{o.price}" rendered="#{not o.editable}" />
</ace:column>
<ace:column headerText="Quantity">
<h:inputText value="#{o.qty}" size="5" rendered="#{o.editable}" />
<h:outputText value="#{o.qty}" rendered="#{not o.editable}" />
</ace:column>
</ace:dataTable>
<br/><br/>
<ice:commandButton value="Save Changes" action="#{orderBean.saveAction}" />
</h:form>
</h:body>
------------------------------managed bean------------------------------
//bean
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import org.icefaces.ace.component.datatable.DataTable;
#ManagedBean(name="orderBean")
#SessionScoped
public class OrderBean implements Serializable{
private static final long serialVersionUID = 1L;
private static final ArrayList<Order> orderList =
new ArrayList<Order>(Arrays.asList( new Order("A0001", "Intel CPU", new BigDecimal("700.00"), 1),
new Order("A0002", "Harddisk 10TB", new BigDecimal("500.00"), 2),
new Order("A0003", "Dell Laptop", new BigDecimal("11600.00"), 8),
new Order("A0004", "Samsung LCD", new BigDecimal("5200.00"), 3),
new Order("A0005", "A4Tech Mouse", new BigDecimal("100.00"), 10)
));
public ArrayList<Order> getOrderList() {
return orderList;
}
public String saveAction() {
//get all existing value but set "editable" to false
for (Order order : orderList){
order.setEditable(false);
}
//return to current page
return null;
}
// listener method
public void editSelectedRow(ValueChangeEvent evt) {
// We get the table object
DataTable table = getParentDatatable((UIComponent) evt.getSource());
if(table==null) return;
// We get the object on the selected line.
Object o = table.getRowData();
Order order = (Order)o;
// Eventually, if you need the index of the line, simply do:
int index = table.getRowIndex();
if(index == 0){
order.setEditable(true);
}
// ...
}
// Method to get the DataTable.
private DataTable getParentDatatable(UIComponent compo) {
if (compo == null) {
return null;
}
if (compo instanceof DataTable) {
return (DataTable) compo;
}
return getParentDatatable(compo.getParent());
}
//managed bean
#ManagedBean(name="order")
public static class Order{
String orderNo;
String productName;
BigDecimal price;
int qty;
boolean editable;
boolean checked;
public Order(){ }
public Order(String orderNo, String productName, BigDecimal price, int qty) {
this.orderNo = orderNo;
this.productName = productName;
this.price = price;
this.qty = qty;
}
//getter and setter methods
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public boolean isEditable() {
return editable;
}
public void setEditable(boolean editable) {
this.editable = editable;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
}
-------------------------------generated html--------------------------------
<table>
<thead>
<tr>
<th class="ui-widget-header"><div
class="ui-header-column clickable" id="j_idt6:j_idt9:j_idt10">
<span><span class="ui-header-text"
id="j_idt6:j_idt9:j_idt10_text">Select</span></span>
</div></th>
<th class="ui-widget-header"><div
class="ui-header-column clickable" id="j_idt6:j_idt9:j_idt12">
<span><span class="ui-header-text"
id="j_idt6:j_idt9:j_idt12_text">Order No</span></span>
</div></th>
<th class="ui-widget-header"><div
class="ui-header-column clickable" id="j_idt6:j_idt9:j_idt15">
<span><span class="ui-header-text"
id="j_idt6:j_idt9:j_idt15_text">Product Name</span></span>
</div></th>
<th class="ui-widget-header"><div
class="ui-header-column clickable" id="j_idt6:j_idt9:j_idt18">
<span><span class="ui-header-text"
id="j_idt6:j_idt9:j_idt18_text">Price</span></span>
</div></th>
<th class="ui-widget-header"><div
class="ui-header-column clickable" id="j_idt6:j_idt9:j_idt21">
<span><span class="ui-header-text"
id="j_idt6:j_idt9:j_idt21_text">Quantity</span></span>
</div></th>
</tr>
</thead>
<tbody class="ui-datatable-data ui-widget-content">
<tr class=" ui-datatable-even " id="j_idt6:j_idt9_row_0" tabindex="0">
<td>
<span>
<input id="j_idt6:myRadioId:_0"
name="j_idt6:myRadioId" onblur="setFocus('');"
onclick=";setFocus('');iceSubmitPartial(form, this, event);"
onfocus="setFocus(this.id);"
onkeypress="Ice.util.radioCheckboxEnter(form,this,event);"
type="radio" value="" />
<label class="iceSelOneRb" for="j_idt6:myRadioId:_0"></label>
</span>
</td>
<td><span id="j_idt6:j_idt9:0:_t14">A0001</span></td>
<td><span id="j_idt6:j_idt9:0:_t17">Intel CPU</span></td>
<td><span id="j_idt6:j_idt9:0:_t20">700.00</span></td>
<td><span id="j_idt6:j_idt9:0:_t23">1</span></td>
</tr>
<tr class=" ui-datatable-odd " id="j_idt6:j_idt9_row_1" tabindex="0">
<td>
<span>
<input id="j_idt6:myRadioId:_0"
name="j_idt6:myRadioId" onblur="setFocus('');"
onclick=";setFocus('');iceSubmitPartial(form, this, event);"
onfocus="setFocus(this.id);"
onkeypress="Ice.util.radioCheckboxEnter(form,this,event);"
type="radio" value="" />
<label class="iceSelOneRb" for="j_idt6:myRadioId:_0"></label>
</span>
</td>
<td><span id="j_idt6:j_idt9:1:_t14">A0002</span></td>
<td><span id="j_idt6:j_idt9:1:_t17">Harddisk 10TB</span></td>
<td><span id="j_idt6:j_idt9:1:_t20">500.00</span></td>
<td><span id="j_idt6:j_idt9:1:_t23">2</span></td>
</tr>
<tr class=" ui-datatable-even " id="j_idt6:j_idt9_row_2" tabindex="0">
<td>
<span>
<input id="j_idt6:myRadioId:_0"
name="j_idt6:myRadioId" onblur="setFocus('');"
onclick=";setFocus('');iceSubmitPartial(form, this, event);"
onfocus="setFocus(this.id);"
onkeypress="Ice.util.radioCheckboxEnter(form,this,event);"
type="radio" value="" />
<label class="iceSelOneRb" for="j_idt6:myRadioId:_0"></label>
</span>
</td>
<td><span id="j_idt6:j_idt9:2:_t14">A0003</span></td>
<td><span id="j_idt6:j_idt9:2:_t17">Dell Laptop</span></td>
<td><span id="j_idt6:j_idt9:2:_t20">11600.00</span></td>
<td><span id="j_idt6:j_idt9:2:_t23">8</span></td>
</tr>
<tr class=" ui-datatable-odd " id="j_idt6:j_idt9_row_3" tabindex="0">
<td>
<span>
<input id="j_idt6:myRadioId:_0"
name="j_idt6:myRadioId" onblur="setFocus('');"
onclick=";setFocus('');iceSubmitPartial(form, this, event);"
onfocus="setFocus(this.id);"
onkeypress="Ice.util.radioCheckboxEnter(form,this,event);"
type="radio" value="" />
<label class="iceSelOneRb" for="j_idt6:myRadioId:_0"></label>
</span>
</td>
<td><span id="j_idt6:j_idt9:3:_t14">A0004</span></td>
<td><span id="j_idt6:j_idt9:3:_t17">Samsung LCD</span></td>
<td><span id="j_idt6:j_idt9:3:_t20">5200.00</span></td>
<td><span id="j_idt6:j_idt9:3:_t23">3</span></td>
</tr>
<tr class=" ui-datatable-even " id="j_idt6:j_idt9_row_4" tabindex="0">
<td>
<span>
<input id="j_idt6:myRadioId:_0"
name="j_idt6:myRadioId" onblur="setFocus('');"
onclick=";setFocus('');iceSubmitPartial(form, this, event);"
onfocus="setFocus(this.id);"
onkeypress="Ice.util.radioCheckboxEnter(form,this,event);"
type="radio" value="" />
<label class="iceSelOneRb" for="j_idt6:myRadioId:_0"></label>
</span>
</td>
<td><span id="j_idt6:j_idt9:4:_t14">A0005</span></td>
<td><span id="j_idt6:j_idt9:4:_t17">A4Tech Mouse</span></td>
<td><span id="j_idt6:j_idt9:4:_t20">100.00</span></td>
<td><span id="j_idt6:j_idt9:4:_t23">10</span></td>
</tr>
</tbody>
</table>
probleme here is that i am unable to call editSelectedRow(ValueChangeEvent evt) method of OrderBean.java
I think what you are missing is the onchange property , do something like the following :
<ice:selectOneRadio id="myRadioId"
value="#{order.checked}" layout="spread"
valueChangeListener="#{orderBean.editSelectedRow}"
onchange="submit()">
<f:selectItem itemValue="" />
</ice:selectOneRadio>
Hope that Helps.

Place Partial View Html elements below the Main View's Html elements

My Goal is to make a CreatePartialCategoryView within an index View. View loads just fine, bet there is just one small problem:
The CreatePartialView html elements are placed above the Index View's html elements. It looks like this at the moment:
Here's the code for the index view:
#model IEnumerable<GUI.Models.CategoryViewModel>
#Html.Partial("~/Views/Category/CreatePartial.cshtml",new GUI.Models.CategoryViewModel());
#{
ViewBag.Title = "Categories";
}
<h2>Categories</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
Here's the code of the CreatePartialView:
#model GUI.Models.CategoryViewModel
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Create a new Category</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
<div class="form-group">
#Html.LabelFor(model => model.Name, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
And this is the code for the CategoryController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using ClassLibrary.Entities;
using DAL;
using GUI.Models;
namespace GUI.Controllers
{
public class CategoryController : Controller
{
private readonly IRepository<Category> _repository;
private CategoryHandler _categoryHandler;
public CategoryController() : this(new Repository<Category>())
{
}
public CategoryController(IRepository<Category> repository)
{
_repository = repository;
_categoryHandler = new CategoryHandler(repository);
}
//
// GET: /Category/
public ActionResult Index()
{
var categories = _categoryHandler.GetAllCategories();
var categoryViewModels = new List<CategoryViewModel>();
foreach (Category category in categories)
{
var viewModel = new CategoryViewModel();
viewModel.Id = category.Id;
viewModel.Name = category.Name;
categoryViewModels.Add(viewModel);
}
//return View();
return View(categoryViewModels);
}
//
// GET: /Category/Details/5
public ActionResult Details(Guid id)
{
return View();
}
//
// GET: /Category/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Category/Create
[HttpPost]
public ActionResult Create(CategoryViewModel model)
{
try
{
// TODO: Add insert logic here
string categoryName = model.Name;
_categoryHandler.AddNewCategory(categoryName);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Category/Edit/5
public ActionResult Edit(Guid id)
{
return View();
}
//
// POST: /Category/Edit/5
[HttpPost]
public ActionResult Edit(Guid id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Category/Delete/5
public ActionResult Delete(Guid id)
{
return View();
}
//
// POST: /Category/Delete/5
[HttpPost]
public ActionResult Delete(Guid id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
So, how do I place the CreatePartialView html elements (Create form) below the index View elements (the list)?
Why don't you just put the line:
#Html.Partial("~/Views/Category/CreatePartial.cshtml",new GUI.Models.CategoryViewModel());
Just below your table listing the categories? like :
<table class="table">
...
#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
#Html.Partial("~/Views/Category/CreatePartial.cshtml",new GUI.Models.CategoryViewModel());
Perhaps I'm misunderstanding you, but don't you just need to move #Html.Partial call below your list in your view?
#model IEnumerable<GUI.Models.CategoryViewModel>
#{
ViewBag.Title = "Categories";
}
<h2>Categories</h2>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
#Html.Partial("~/Views/Category/CreatePartial.cshtml",new GUI.Models.CategoryViewModel());

I can not get dropdownlist value in mvc3

i have a form by dropdownlist element. i want get value of dropdownlist in controller.
i do not know how can do this. i read my username of users with dataview in controller and want to change role of them by dropdownlist options.
#using (Html.BeginForm()) {
<form name="register" action="#">
<div>
<table>
#foreach (MembershipUser user in Model)
{
var userroles = Roles.GetRolesForUser(user.UserName);
<tr>
<td>
#user.UserName
</td>
#foreach (var role in userroles)
{
<td>
#role
</td>
}
<td>
#Html.DropDownList("roleName")
#Html.ValidationMessage("roleName")
</td>
<td>
</td>
</tr>
}
</table>
<input type="submit" class="register" value="Save" />
</div>
</form>
}
and this is my controller:
public ActionResult Index()
{
ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName");
return View(Membership.GetAllUsers());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index()
{
return RedirectToAction("Index", "Home");
}
Did you try:
public ActionResult Index(string roleNameSelectedValue)
{
// roleNameSelectedValue is the "Value" field of selected item in your dropdownlist
return RedirectToAction("Index", "Home");
}
Or using #Html.DropDownListFor(m => Model.roleName, ViewBag.roleName as SelectList)

<h:commandButton> action is not invoked

I've got a bit of a problem. Basically I have a page with a form containing an input text field, a search button and a ui:repeat structure that displays the results of the search using more input text fields and a couple of other buttons (the idea is to list all search hits and allow the user to edit them directly from the same page). Now, the search and the display part works well - it does what it's supposed to. However, when I try to click on one of the buttons that are displayed in the ui:repeat section, the action isn't invoked.
When I press the search button (Pretraga in code) a function is called in the Kupac bean which then gets the results from the database and places them into a list and finally redirects back to the same page (from my understanding this will load the page again) and lists the results using the ui:repeat component. But as I said, when I press the other buttons (Izmeni and Glavna in code) they do nothing. Kupac bean is #RequestScoped and I already tried changing it to #ViewScoped, however this only made it worse by having the search not work as well (i.e. it will display nothing).
Here's the JSF page code:
<?xml version='1.0' encoding='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"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Pregled kupaca</title>
</h:head>
<h:body>
<h3 align="center">Pregled kupaca</h3>
<h:form>
<table align="center">
<tr>
<td align="center" colspan="2"><h:link outcome="pocetna.xhtml">Pocetna</h:link></td>
</tr>
<tr>
<td>Firma</td>
<td><h:inputText value="#{kupac.naziv_firme}"/></td>
</tr>
<tr>
<td align="center" colspan="2">
<h:commandButton action="#{kupac.listKupci()}" value="Pretraga">
</h:commandButton>
</td>
</tr>
</table>
<br/>
<ui:repeat value="#{kupac.zeljeni_kupci}" var="kupac">
<table align="center" border="1">
<tr>
<td>Ime i prezime</td>
<td><h:inputText
required="true"
requiredMessage="Niste uneli ime i prezime!"
value="#{kupac.ime_prezime}"/></td>
</tr>
<tr>
<td>Adresa</td>
<td><h:inputText
required="true"
requiredMessage="Niste uneli adresu!"
value="#{kupac.adresa}"/></td>
</tr>
<tr>
<td>Naziv firme</td>
<td><h:inputText
required="true"
requiredMessage="Niste uneli naziv firme!"
value="#{kupac.naziv_firme}"/></td>
</tr>
<tr>
<td>Adresa firme</td>
<td><h:inputText
required="true"
requiredMessage="Niste uneli adresu firme!"
value="#{kupac.adresa_firme}"/></td>
</tr>
<tr>
<td>Br. telefona</td>
<td><h:inputText
required="true"
requiredMessage="Niste uneli broj telefona!"
value="#{kupac.br_tel}"/></td>
</tr>
<tr>
<td>
<h:commandButton value="Izmeni" action="#{kupac.izmenaKupca()}"/>
</td>
<td><h:commandButton action="#{kupac.test()}" value="Glavna"/></td>
</tr>
</table>
<br/><br/>
</ui:repeat>
</h:form>
</h:body>
</html>
and here's the full bean code in Java:
package beans;
import exceptions.DBException;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import util.Database;
#ManagedBean
#RequestScoped
public class Kupac implements Serializable {
private int id_kupac;
private String ime_prezime;
private String adresa;
private String naziv_firme;
private String adresa_firme;
private String br_tel;
private List<Kupac> svi_kupci;
private List<Kupac> zeljeni_kupci;
public void init() {
listKupci();
}
public void poruka() {
System.out.println("ID "+id_kupac);
}
public String test() {
return "pocetna";
}
public String izmenaKupca() throws DBException {
Kupac kupac = new Kupac();
Database db = Database.getInstance();
kupac.setId_kupac(id_kupac);
kupac.setIme_prezime(ime_prezime);
kupac.setAdresa(adresa);
kupac.setNaziv_firme(naziv_firme);
kupac.setAdresa_firme(adresa_firme);
kupac.setBr_tel(br_tel);
try {
db.updateKupac(kupac);
} catch (DBException ex) {
return "error";
}
return "pregled_kupaca";
}
public String unosKupca() throws DBException {
Kupac kupac = new Kupac();
Database db = Database.getInstance();
kupac.setIme_prezime(ime_prezime);
kupac.setAdresa(adresa);
kupac.setNaziv_firme(naziv_firme);
kupac.setAdresa_firme(adresa_firme);
kupac.setBr_tel(br_tel);
try {
db.insertKupac(kupac);
} catch (DBException ex) {
return "error";
}
return "pocetna";
}
public String listKupci() {
Database db = Database.getInstance();
zeljeni_kupci = new LinkedList<Kupac>();
try {
svi_kupci = db.listKupci();
for (Kupac k : svi_kupci) {
if (k.naziv_firme.equals(naziv_firme) || "".equals(naziv_firme)) {
zeljeni_kupci.add(k);
}
}
} catch (DBException ex) {
return "error";
}
return "pregled_kupaca";
}
public List<Kupac> getZeljeni_kupci() {
return zeljeni_kupci;
}
public void setZeljeni_kupci(List<Kupac> zeljeni_kupci) {
this.zeljeni_kupci = zeljeni_kupci;
}
public List<Kupac> getSvi_kupci() {
return svi_kupci;
}
public void setSvi_kupci(List<Kupac> svi_kupci) {
this.svi_kupci = svi_kupci;
}
public int getId_kupac() {
return id_kupac;
}
public void setId_kupac(int id_kupac) {
this.id_kupac = id_kupac;
}
public String getIme_prezime() {
return ime_prezime;
}
public void setIme_prezime(String ime_prezime) {
this.ime_prezime = ime_prezime;
}
public String getAdresa() {
return adresa;
}
public void setAdresa(String adresa) {
this.adresa = adresa;
}
public String getNaziv_firme() {
return naziv_firme;
}
public void setNaziv_firme(String naziv_firme) {
this.naziv_firme = naziv_firme;
}
public String getAdresa_firme() {
return adresa_firme;
}
public void setAdresa_firme(String adresa_firme) {
this.adresa_firme = adresa_firme;
}
public String getBr_tel() {
return br_tel;
}
public void setBr_tel(String br_tel) {
this.br_tel = br_tel;
}
/**
* Creates a new instance of Kupac
*/
public Kupac() {
}
}
I'm using JSF 2.2 on a GlassFish 4.0 server using Netbeans 7 IDE.
Sorry for the long post and the fact that this question has now been asked several times, but I haven't managed to fix it for 2 hrs now and would appreciate any help. Cheers!

Resources