This question already has an answer here:
How pass parameter when navigate to another page (Shell)
(1 answer)
Closed 4 months ago.
I have main page on which I have collection view. I want to navigate to next page after I click submit button .I am able to navigate to the next page but I also want total list of items which I have in my collection how can I achieve that?
I don't know the detail of your code, but you can try to pass the total list of items as the parameter of the next page.(suppose it's name is SecondPage)
You can refer to the following code:
MainPage.cs
public partial class MainPage : ContentPage
{
MyViewModel myViewModel;
public MainPage()
{
InitializeComponent();
myViewModel = new MyViewModel();
BindingContext = myViewModel;
}
private void mCollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string previous = (e.PreviousSelection.FirstOrDefault() as MyModel)?.Name;
string current = (e.CurrentSelection.FirstOrDefault() as MyModel)?.Name;
}
private async void Button_Clicked(object sender, EventArgs e)
{
// here we can pass the data we need.
var secondPage = new SecondPage(myViewModel.Data);
await Navigation.PushAsync(secondPage);
}
}
MainPage.xaml.cs
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiCollectionApp.MainPage"
xmlns:local="clr-namespace:MauiCollectionApp"
x:Name="myPage"
>
<VerticalStackLayout>
<CollectionView ItemsSource="{ Binding Data}" x:Name="mCollectionView"
SelectionChanged="mCollectionView_SelectionChanged"
SelectionMode="Single"
>
<CollectionView.ItemTemplate>
<DataTemplate>
<HorizontalStackLayout Margin="3" >
<Label Text="{Binding Name}" BackgroundColor="Gray"/>
<Label Text="{Binding Car.Make}" Margin="5,0,5,0" />
<Button Text="delete" Margin="10,0,0,0"
BackgroundColor="Red"
Command="{Binding Path= BindingContext.RemoveEquipmentCommand,Source={Reference mCollectionView }}" CommandParameter="{Binding .}"
/>
</HorizontalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="submit" Clicked="Button_Clicked" Margin="10"/>
</VerticalStackLayout>
</ContentPage>
MyViewModel.cs
public class MyViewModel: INotifyPropertyChanged
{
public ObservableCollection<MyModel> Data { get; set; }
public ICommand RemoveEquipmentCommand => new Command<MyModel>(ReMoveItem);
private void ReMoveItem(MyModel obj)
{
System.Diagnostics.Debug.WriteLine(" the selected item's name is: " + obj.Name );
Data.Remove(obj);
}
public MyViewModel() {
Data = new ObservableCollection<MyModel>();
Data.Add(new MyModel { Name ="model_1", Car= new Vehicle {Make="Make1" } });
Data.Add(new MyModel { Name = "model_2", Car = new Vehicle { Make = "Make2" } });
Data.Add(new MyModel { Name = "model_3", Car = new Vehicle { Make = "Make3" } });
Data.Add(new MyModel { Name = "model_4", Car = new Vehicle { Make = "Make4" } });
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
SecondPage.cs
public partial class SecondPage : ContentPage
{
public ObservableCollection<MyModel> Items { get; set; }
public SecondPage(ObservableCollection<MyModel> data )
{ // we can get the passed data here
InitializeComponent();
this.Items = data;
}
}
In my ViewModel i want o load the Picker source RegionName data from an Azure Region table. I extract data from table in an async method but the Picker displays an empty List even after ObservableCollection or List has changed or crashes.
When using PropertyChanged on the ListRegion list itself the app crashes.
In my models:
public class Region
{
public string Id { get; set; }
public string RegionName { get; set; }
}
In my RegisterPage.xaml:
<Picker SelectedIndex="{Binding RegionsSelectedIndex, Mode=TwoWay}"
ItemsSource="{Binding Regions}"
Margin="0,15,0,0"
Title="Select a region">
</Picker>
in my RegisterPage.cs:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class RegisterPage : ContentPage
{
RegisterViewModel registerVM;
public RegisterPage ()
{
InitializeComponent ();
registerVM = new RegisterViewModel();
this.BindingContext = registerVM;
}
protected override void OnAppearing()
{
base.OnAppearing();
}
in my RegisterPageViewModel:
public class RegisterViewModel: INotifyPropertyChanged
{
ApiServices _apiServices = new ApiServices();
public RegisterViewModel()
{
initializePickerAsync();
}
async private void initializePickerAsync()
{
try
{
var regionsList = await App.MobileService.GetTable<Models.Region>().ToListAsync();
List<string> regionsStringList = new List<string>();
foreach (Models.Region reg in regionsList)
{
regionsStringList.Add(reg.RegionName);
}
Regions = regionsStringList;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/*
private RegisterViewModel (ObservableCollection<Models.Region> regionData)
{
ObservableCollection<string> regionDataAsStringList = new ObservableCollection<string>();
foreach (Models.Region r in regionData)
{
regionDataAsStringList.Add(r.RegionName);
}
this.Regions = regionDataAsStringList;
}
async public static Task<RegisterViewModel> BuildViewModelAsync()
{
ObservableCollection<Models.Region> tmpRegionData = new ObservableCollection<Models.Region>(await App.MobileService.GetTable<Models.Region>().ToListAsync());
return new RegisterViewModel(tmpRegionData);
}
*/
int regionsSelectedIndex = 0;
public int RegionsSelectedIndex
{
get
{
return regionsSelectedIndex;
}
set
{
if (regionsSelectedIndex != value)
{
regionsSelectedIndex = value;
OnPropertyChanged(nameof(RegionsSelectedIndex));
if (regionsSelectedIndex >= 0)
{
Region = Regions[regionsSelectedIndex];
}
}
}
}
// public ObservableCollection<Region> Regions {get;set}
public List<string> Regions
{
get
{
return Regions;
}
set
{
if (Regions != value)
{
Regions = value;
OnPropertyChanged(nameof(Regions));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
you seem to be doing a lot of unnecessary creating and assigning of different lists of data. You should be able to create your ObservableCollection ONCE and then add your data to it, something like this
in your ViewModel
ObservableCollection<Region> Regions { get; set; }
public RegisterViewModel()
{
Regions = new ObservableCollection<Region>();
}
public async void GetData()
{
var regionsList = await App.MobileService.GetTable<Models.Region>().ToListAsync();
foreach (Models.Region reg in regionsList)
{
Regions.Add(reg);
}
}
in your page
protected async override void OnAppearing()
{
base.OnAppearing();
await registerVM.GetData();
}
New to Xamarin development! Using Visual Studio 2017 and all the latest installs and updates for my dev environment.
I have my app "shell" working in that it will run, navigate, crud to local db, and sync to rest services. So, the base of my app is sound. I am trying to integrate the ZXing barcode scanner into my app. Most of what help I can find relates to raw forms or code behind xaml. I am using views and view models and I don't know how to translate the information I am finding to this model.
I currently have the xaml view showing the "camera viewer" when a button is clicked so I can see a barcode using the camera. However, there is no "red line" in the camera view and subsequently, there are no events being fired to let me know what is going on. I really am confused as I am so new to Xamarin. I don't know where to start.
XAML View page
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
xmlns:fe="clr-namespace:FreshEssentials;assembly=FreshEssentials"
xmlns:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="Views.InventoryPage1"
Title="Inventory Page">
<StackLayout Spacing="5" Padding="10,10,10,0">
<!--<fe:BindablePicker ItemsSource="{Binding Areas}" SelectedItem="{Binding SelectedArea}" DisplayProperty="AreaName" Title="Select Your Area" />
<fe:BindablePicker ItemsSource="{Binding Reasons}" SelectedItem="{Binding SelectedReason}"
DisplayProperty="Description" Title="Select a Reason" />
<Label Text="Scan"/>
<Entry Text="{Binding Barcode}"/>
<Label Text="Quantity"/>
<Entry Text="{Binding Quantity}"/>
<Button Text="Save" Style="{StaticResource Button_Primary}" Command="{Binding SaveCommand}" />-->
<forms:ZXingScannerView WidthRequest="100" HeightRequest="100" IsScanning="{Binding IsScanning}" IsAnalyzing="{Binding IsAnalyzing}" Result="{Binding Result, Mode=TwoWay}" ScanResultCommand="{Binding QRScanResultCommand}" ></forms:ZXingScannerView>
</StackLayout>
</ContentPage>
ViewModel
public class InventoryPage1ViewModel : BindableBase, INavigationAware
{
private readonly IPageDialogService _pageDialogService;
private bool _isAnalyzing = true;
private bool _isScanning = true;
public ZXing.Result Result { get; set; }
public List<Area> Areas { get; private set; }
public Area SelectedArea { get; set; }
public List<Reason> Reasons { get; private set; }
public Reason SelectedReason { get; set; }
public int Quantity { get; set; }
public string Barcode { get; set; }
public DelegateCommand SaveCommand => new DelegateCommand(PerformSave);
public DelegateCommand QRScanResultCommand => new DelegateCommand(QRCommand);
private readonly IAreaService _areaService;
private readonly IScanService _scanService;
private readonly IReasonService _reasonService;
public InventoryPage1ViewModel(IAreaService areaService, IScanService scanService, IReasonService reasonService, IPageDialogService pageDialogService)
{
_pageDialogService = pageDialogService;
_reasonService = reasonService;
_scanService = scanService;
_areaService = areaService;
Areas = _areaService.GetAll();
Reasons = _reasonService.GetAll();
}
public bool IsScanning
{
get
{
return _isScanning;
}
set
{
_isScanning = value;
RaisePropertyChanged();
}
}
public bool IsAnalyzing
{
get
{
return _isAnalyzing;
}
set
{
_isAnalyzing = value;
RaisePropertyChanged();
}
}
private void QRCommand()
{
int x = 1;
}
private async void PerformSave()
{
var scan = new Scan()
{
AreaId = SelectedArea.Id,
InsertDateTime = DateTime.Now,
ReasonId = SelectedReason.Id,
ScanItem = Barcode,
ScanQty = Quantity,
IsUploaded = false
};
// Save it to the DB here.
var retVal = _scanService.Insert(scan);
if (retVal)
{
await _pageDialogService.DisplayAlertAsync("Saved", "Scan saved successfully.", "OK");
}
else
{
// TODO: Inform the user something went wrong.
}
}
int _index;
public int SelectIndex
{
get
{
return _index;
}
set
{
_index = value;
RaisePropertyChanged("SelectIndex");
}
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
public void OnNavigatedTo(NavigationParameters parameters)
{
}
public void OnNavigatingTo(NavigationParameters parameters)
{
}
}
MainActivity
public class MainActivity :
global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.tabs;
ToolbarResource = Resource.Layout.toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
LoadApplication(new App(new AndroidInitializer()));
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IConnectionFactory, ConnectionFactory>();
}
}
UPDATE
I have a working view model now thanks to #Krzysztof over at Xamarin.Forms.
New ViewModel
public class InventoryPage1ViewModel : BindableBase, INavigationAware
{
private readonly IPageDialogService _pageDialogService;
public List<Area> Areas { get; private set; }
public Area SelectedArea { get; set; }
public List<Reason> Reasons { get; private set; }
public Reason SelectedReason { get; set; }
public int Quantity { get; set; }
public DelegateCommand SaveCommand => new DelegateCommand(PerformSave);
private readonly IAreaService _areaService;
private readonly IScanService _scanService;
private readonly IReasonService _reasonService;
public InventoryPage1ViewModel(IAreaService areaService, IScanService scanService, IReasonService reasonService, IPageDialogService pageDialogService)
{
_pageDialogService = pageDialogService;
_reasonService = reasonService;
_scanService = scanService;
_areaService = areaService;
Areas = _areaService.GetAll();
Reasons = _reasonService.GetAll();
}
public ZXing.Result Result { get; set; }
private string barcode = string.Empty;
public string Barcode
{
get
{
return barcode;
}
set
{
barcode = value;
RaisePropertyChanged();
}
}
private bool isAnalyzing = true;
public bool IsAnalyzing
{
get { return this.isAnalyzing; }
set
{
if (!bool.Equals(this.isAnalyzing, value))
{
this.isAnalyzing = value;
RaisePropertyChanged(nameof(IsAnalyzing));
}
}
}
private bool isScanning = true;
public bool IsScanning
{
get { return this.isScanning; }
set
{
if (!bool.Equals(this.isScanning, value))
{
this.isScanning = value;
RaisePropertyChanged(nameof(IsScanning));
}
}
}
public Command QRScanResultCommand
{
get
{
return new Command(() =>
{
IsAnalyzing = false;
IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
{
Barcode = Result.Text;
await _pageDialogService.DisplayAlertAsync("Scanned Item", Result.Text, "Ok");
});
IsAnalyzing = true;
IsScanning = true;
});
}
}
private async void PerformSave()
{
var scan = new Scan()
{
AreaId = SelectedArea.Id,
InsertDateTime = DateTime.Now,
ReasonId = SelectedReason.Id,
ScanItem = Barcode,
ScanQty = Quantity,
IsUploaded = false
};
// Save it to the DB here.
var retVal = _scanService.Insert(scan);
if (retVal)
{
await _pageDialogService.DisplayAlertAsync("Saved", "Scan saved successfully.", "OK");
}
else
{
// TODO: Inform the user something went wrong.
}
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
public void OnNavigatedTo(NavigationParameters parameters)
{
}
public void OnNavigatingTo(NavigationParameters parameters)
{
}
}
I have no idea why my forms behave in such way.
This is my JSF page:
<h:body>
<h:form>
<h:form>
<h:selectOneMenu value="#{productBean.product}" converter="#{productConverter}" validator="com.jsf.ProductAvailableValidator">
<f:selectItems value="#{productBean.pizza}" var="pizza" itemValue="#{pizza}" itemLabel="#{pizza.name}" />
<h:commandButton value="Dodaj" action="#{productBean.addToOrder(productBean.product.name)}" /></h:selectOneMenu>
</h:form>
<h:form>
<h:selectOneMenu value="#{productBean.product}" converter="#{productConverter}" validator="com.jsf.ProductAvailableValidator">
<f:selectItems value="#{productBean.drink}" var="drink" itemValue="#{drink}" itemLabel="#{drink.name}" />
<h:commandButton value="Dodaj" action="#{productBean.addToOrder(productBean.product.name)}" /></h:selectOneMenu>
</h:form>
<h:form>
<h:selectOneMenu value="#{productBean.product}" converter="#{productConverter}" validator="com.jsf.ProductAvailableValidator">
<f:selectItems value="#{productBean.other}" var="other" itemValue="#{other}" itemLabel="#{other.name}" />
<h:commandButton value="Dodaj" action="#{productBean.addToOrder(productBean.product.name)}" /></h:selectOneMenu>
</h:form>
<messages />
<h:outputText value="#{productBean.order}" />
<h:commandButton value="Wyczyść" action="#{ProductBean.clearOrder()}" /></h:form>
</h:body>
And this is my ProductBean:
#ManagedBean
#SessionScoped
public class ProductBean extends Connector
{
private List<Product> products;
private List<Product> pizza;
private List<Product> drink;
private List<Product> other;
boolean first = true;
private StringBuilder order = new StringBuilder();
public String getOrder() {
return order.toString();
}
private Product product;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public void addToOrder(String prod)
{
System.out.println("dodaje");
if(first)
{
first = false;
this.order.append(prod);
}
else
this.order.append(" + ").append(prod);
}
public void clearOrder()
{
this.order = null;
first = true;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public ProductBean() throws SQLException
{
resultSet = statement.executeQuery("SELECT * FROM dbo.products");
products = new ArrayList<Product>();
while(resultSet.next())
{
product = new Product();
product.setId_product(resultSet.getInt("id_product"));
product.setName(resultSet.getString("name"));
product.setCategory(resultSet.getInt("category_id"));
product.setIs_available(resultSet.getInt("is_available"));
products.add(product);
}
}
public Product getProductById(int id)
{
Iterator<Product> it = products.iterator();
while(it.hasNext())
{
Product prod = it.next();
if(prod.getId_product() == id)
return prod;
}
return null;
}
public List<Product> getPizza() throws SQLException
{
Iterator<Product> it = products.iterator();
pizza = new ArrayList<Product>();
while(it.hasNext())
{
Product prod = it.next();
if(prod.getCategory() == 1)
pizza.add(prod);
}
return pizza;
}
public List<Product> getDrink() throws SQLException
{
Iterator<Product> it = products.iterator();
drink = new ArrayList<Product>();
while(it.hasNext())
{
Product prod = it.next();
if(prod.getCategory() == 2)
drink.add(prod);
}
return drink;
}
public List<Product> getOther() throws SQLException
{
Iterator<Product> it = products.iterator();
other = new ArrayList<Product>();
while(it.hasNext())
{
Product prod = it.next();
if(prod.getCategory() == 3)
other.add(prod);
}
return other;
}
public List<Product> getProducts() {
return products;
}
}
I also send a screenshot here to make code easier and faster to analize:
What happens here is that only the first button "Dodaj" (which means "add") works and add the String in outputlabel correctly. The rest of them do nothing. When I change the order, again only the first one works. Why?
You have multiple nested/cascaded <h:form>'s, that is not allowed in HTML! Either make one <h:form> and put all elements in that form, or make multiple <h:form>'s, but don't nest/cascade them!
I'm really in a grave problem with this. All of a sudden my properly working code has stopped working. I have no clue what so ever why!!!! And worst of all I have to deploy my project today :( . Don't know if it will be right to say or not but all of this started 2 days after adding PrimeFaces in build path. Can anyone please direct me to the right direction. Would be great help!
I have following configuration:
Glassfish v3
Mojara 2.1.6-FCS
JPA Eclipselink
Controller Bean
public List<Usergroupdetail> getUsergroupdetail_list() {
List<Usergroupdetail> myUserGroupDetail = new ArrayList<Usergroupdetail>(
lODBN.listUserGroupDetail(loginBean.getUsername()));
System.out.println("username found is:" + loginBean.getUsername());
return myUserGroupDetail;
}
For getting complete list of data from the JPA PoJO UserGroupReport
public List<Usergroupreport> getGroupId_list() {
List<Usergroupreport> myAllgroupIds = new ArrayList<Usergroupreport>(
lODBN.findAllGroupIdByUser(loginBean.getUsername()));
return myAllgroupIds;
}
Stack Trace
WARNING: PWC4011: Unable to set request character encoding to ISO-8859-1 from context /WebApp, because request parameters have already been read, or ServletRequest.getReader() has already been called
FINE: SELECT ROWID, GROUPID, GROUPNAME, USERNAME FROM usergroupdetail WHERE (USERNAME = ?)
bind => [1 parameter bound]
INFO: []
INFO: username found is:null
FINE: SELECT ROWID, GROUPID, GROUPNAME, USERNAME FROM usergroupdetail WHERE (USERNAME = ?)
bind => [1 parameter bound]
INFO: []
INFO: username found is:null
FINE: SELECT ROWID, GROUPID, GROUPNAME, USERNAME FROM usergroupdetail WHERE (USERNAME = ?)
bind => [1 parameter bound]
INFO: []
INFO: username found is:null
FINE: SELECT RowId, LOD1COSTCENTER, LOD1DISPLAYNAME, LOD1DOMAIN, LOD1MAIL, LOD1USER, LOD2COSTCENTER, LOD2DISPLAYNAME, LOD2DOMAIN, LOD2MAIL, LOD2USER, Access, AuthentifizierteBenutzer, Auto, Comment, Comment1, Comment2, Comment3, Domain1, Domain2, EmailFeedback, EmailSendStatus, RechteGruppeChange, RechteGruppeRead, Security, Type, Username FROM lodreport WHERE (Username = ?)
bind => [1 parameter bound]
INFO: []
INFO: LOD list for Username :null
FINE: SELECT RowId, LOD1COSTCENTER, LOD1DISPLAYNAME, LOD1DOMAIN, LOD1MAIL, LOD1USER, LOD2COSTCENTER, LOD2DISPLAYNAME, LOD2DOMAIN, LOD2MAIL, LOD2USER, Access, AuthentifizierteBenutzer, Auto, Comment, Comment1, Comment2, Comment3, Domain1, Domain2, EmailFeedback, EmailSendStatus, RechteGruppeChange, RechteGruppeRead, Security, Type, Username FROM lodreport WHERE (Username = ?)
bind => [1 parameter bound]
INFO: []
INFO: LOD list for Username :null
FINE: SELECT RowId, LOD1COSTCENTER, LOD1DISPLAYNAME, LOD1DOMAIN, LOD1MAIL, LOD1USER, LOD2COSTCENTER, LOD2DISPLAYNAME, LOD2DOMAIN, LOD2MAIL, LOD2USER, Access, AuthentifizierteBenutzer, Auto, Comment, Comment1, Comment2, Comment3, Domain1, Domain2, EmailFeedback, EmailSendStatus, RechteGruppeChange, RechteGruppeRead, Security, Type, Username FROM lodreport WHERE (Username = ?)
bind => [1 parameter bound]
INFO: []
INFO: LOD list for Username :null
Login Bean
package bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
//import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import ejb.UserDaoBean;
import ejb.UserGroupDaoBean;
import model.User;
#ManagedBean(name = "loginBean")
#SessionScoped
public class LoginBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#EJB
private UserDaoBean uDB;
#EJB
private UserGroupDaoBean uGDB;
private User userId;
public List<User> usernameFirstLastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String firstName;
public String lastName;
public String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<User> getUsernameFirstLastName() {
List<User> myName = new ArrayList<User>(uDB.findFirtLastNames(username));
return myName;
}
public void setUsernameFirstLastName(List<User> usernameFirstLastName) {
this.usernameFirstLastName = usernameFirstLastName;
}
private String username;
public User getUserId() {
return userId;
}
public void setUserId(User userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String login() {
FacesContext context = FacesContext.getCurrentInstance();
if (uDB.validateUser(username,password)) {
userId = uDB.findUser(username);
context.getExternalContext().getSessionMap().put("userId", userId);
if (uGDB.validateGroup(userId)) {
return "home.jsf?faces-redirect=true&includeViewParams=true";
}
return "normalHome.jsf?faces-redirect=true&includeViewParams=true";
} else {
FacesMessage message = new FacesMessage();
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("Username doesn't exists! OR User is trying to login from someone else's account");
context.addMessage("", message);
return "newloginerror.jsf?faces-redirect=true";
}
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext()
.invalidateSession();
return "logout.jsf?faces-redirect=true";
}
}
Login Page
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="./css/PageLayout.css" rel="stylesheet" type="text/css" />
<title>Login</title>
</h:head>
<h:body>
<f:view>
<div style="background-color: #205a8c; width: auto; height: 60px">
<h3 style="font-size: large; position: relative;" align="left">Lord
Of Data Web App</h3>
</div>
<div id="wrappers">
<div class="content pls centering">
<h:form>
<table class="form_table" style="background-color: silver;">
<tbody>
<tr>
<td>Username</td>
<td><h:inputText id="inputusername"
value="#{loginBean.username}" required="true"
requiredMessage="Username is required!"></h:inputText> <h:message
for="inputusername"></h:message></td>
</tr>
<tr>
<td>Password</td>
<td><h:inputSecret id="inputpassword"
value="#{loginBean.password}" required="true"
requiredMessage="Password is required!"></h:inputSecret> <h:message
for="inputpassword"></h:message></td>
</tr>
</tbody>
</table>
<div id="buttonsoptions" align="center" style="padding-top: 10px;">
<h:panelGroup>
<tr>
<td><h:commandButton id="login" value="Login"
action="#{loginBean.login}"></h:commandButton></td>
</tr>
</h:panelGroup>
</div>
</h:form>
</div>
</div>
</f:view>
</h:body>
</html>
EJB Code Snippet for Login Authentication
public boolean validateUser(String username, String password) {
try {
Query myQuery = entityManager.createNamedQuery("userverification")
.setParameter("username", username)
.setParameter("password", password);
User result = (User) myQuery.getSingleResult();
if (result != null) {
System.out.println("Loggin sucessful!");
return true;
} else {
System.out.println("User does not exists in the system");
return false;
}
} catch (NoResultException e) {
return false;
}
}
And I tried to use the username to my other session bean which was working perfectly fine until two days back. Here is the code for this bean where I am trying to call the Username picked from login bean
Bean
package bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import ejb.LODReportDaoBean;
import ejb.LordOfDataDaoBeanNormal;
import ejb.UserDaoBean;
import model.Lodreport;
import model.Usergroupdetail;
import model.Usergroupreport;
#ManagedBean(name = "lordOfDataNormalUserBean")
#SessionScoped
public class LordOfDataNormalUserBean implements Serializable {
/**
* #author Sushant Pandey
*/
private static final long serialVersionUID = 1L;
#EJB
private LordOfDataDaoBeanNormal lODBN;
#EJB
private UserDaoBean uDB;
#EJB
private LODReportDaoBean lONRDB;
#ManagedProperty(value = "#{loginBean}")
private LoginBean loginBean;
public LoginBean getLoginBean() {
return loginBean;
}
public void setLoginBean(LoginBean loginBean) {
this.loginBean = loginBean;
}
public List<Lodreport> lodnormal_list;
public List<Usergroupreport> usergroup_list;
public List<Usergroupdetail> usergroupdetail_list;
public List<Lodreport> findDataByRowId;
private Lodreport myreport = new Lodreport();
public Lodreport getMyreport() {
return myreport;
}
public void setMyreport(Lodreport myreport) {
this.myreport = myreport;
}
public void setFindDataByRowId(List<Lodreport> findDataByRowId) {
this.findDataByRowId = findDataByRowId;
}
public int security;
public String username;
public String access;
public String authentifizierteBenutzer;
public String auto;
public String comment;
public String comment1;
public String comment2;
public String comment3;
public String domain1;
public String domain2;
public String emailFeedback;
public String emailSendStatus;
public String lOD1CostCenter;
public String lOD1DisplayName;
public String lOD1Domain;
public String lOD1Mail;
public String lOD1User;
public String lOD2CostCenter;
public String lOD2DisplayName;
public String lOD2Domain;
public String lOD2Mail;
public String lOD2User;
public String rechteGruppeChange;
public String rechteGruppeRead;
public String type;
public int rowId;
public List<Usergroupreport> groupId_list;
public boolean edit;
public boolean isEdit() {
return edit;
}
public void setEdit(boolean edit) {
this.edit = edit;
}
public void setGroupId_list(List<Usergroupreport> groupId_list) {
this.groupId_list = groupId_list;
}
public void setLodnormal_list(List<Lodreport> lodnormal_list) {
this.lodnormal_list = lodnormal_list;
}
public int getSecurity() {
return security;
}
public void setSecurity(int security) {
this.security = security;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAccess() {
return access;
}
public void setAccess(String access) {
this.access = access;
}
public String getAuthentifizierteBenutzer() {
return authentifizierteBenutzer;
}
public void setAuthentifizierteBenutzer(String authentifizierteBenutzer) {
this.authentifizierteBenutzer = authentifizierteBenutzer;
}
public String getAuto() {
return auto;
}
public void setAuto(String auto) {
this.auto = auto;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getComment1() {
return comment1;
}
public void setComment1(String comment1) {
this.comment1 = comment1;
}
public String getComment2() {
return comment2;
}
public void setComment2(String comment2) {
this.comment2 = comment2;
}
public String getComment3() {
return comment3;
}
public void setComment3(String comment3) {
this.comment3 = comment3;
}
public String getDomain1() {
return domain1;
}
public void setDomain1(String domain1) {
this.domain1 = domain1;
}
public String getDomain2() {
return domain2;
}
public void setDomain2(String domain2) {
this.domain2 = domain2;
}
public String getEmailFeedback() {
return emailFeedback;
}
public void setEmailFeedback(String emailFeedback) {
this.emailFeedback = emailFeedback;
}
public String getEmailSendStatus() {
return emailSendStatus;
}
public void setEmailSendStatus(String emailSendStatus) {
this.emailSendStatus = emailSendStatus;
}
public String getlOD1CostCenter() {
return lOD1CostCenter;
}
public void setlOD1CostCenter(String lOD1CostCenter) {
this.lOD1CostCenter = lOD1CostCenter;
}
public String getlOD1DisplayName() {
return lOD1DisplayName;
}
public void setlOD1DisplayName(String lOD1DisplayName) {
this.lOD1DisplayName = lOD1DisplayName;
}
public String getlOD1Domain() {
return lOD1Domain;
}
public void setlOD1Domain(String lOD1Domain) {
this.lOD1Domain = lOD1Domain;
}
public String getlOD1Mail() {
return lOD1Mail;
}
public void setlOD1Mail(String lOD1Mail) {
this.lOD1Mail = lOD1Mail;
}
public String getlOD1User() {
return lOD1User;
}
public void setlOD1User(String lOD1User) {
this.lOD1User = lOD1User;
}
public String getlOD2CostCenter() {
return lOD2CostCenter;
}
public void setlOD2CostCenter(String lOD2CostCenter) {
this.lOD2CostCenter = lOD2CostCenter;
}
public String getlOD2DisplayName() {
return lOD2DisplayName;
}
public void setlOD2DisplayName(String lOD2DisplayName) {
this.lOD2DisplayName = lOD2DisplayName;
}
public String getlOD2Domain() {
return lOD2Domain;
}
public void setlOD2Domain(String lOD2Domain) {
this.lOD2Domain = lOD2Domain;
}
public String getlOD2Mail() {
return lOD2Mail;
}
public void setlOD2Mail(String lOD2Mail) {
this.lOD2Mail = lOD2Mail;
}
public String getlOD2User() {
return lOD2User;
}
public void setlOD2User(String lOD2User) {
this.lOD2User = lOD2User;
}
public String getRechteGruppeChange() {
return rechteGruppeChange;
}
public void setRechteGruppeChange(String rechteGruppeChange) {
this.rechteGruppeChange = rechteGruppeChange;
}
public String getRechteGruppeRead() {
return rechteGruppeRead;
}
public void setRechteGruppeRead(String rechteGruppeRead) {
this.rechteGruppeRead = rechteGruppeRead;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getRowId() {
return rowId;
}
public void setRowId(int rowId) {
this.rowId = rowId;
}
public void setUsergroup_list(List<Usergroupreport> usergroup_list) {
this.usergroup_list = usergroup_list;
}
public void setUsergroupdetail_list(
List<Usergroupdetail> usergroupdetail_list) {
this.usergroupdetail_list = usergroupdetail_list;
}
#PostConstruct
public void init() {
// getLodnormal_list();
getUsergroupdetail_list();
// getGroupId_list();
getUsername();
}
public String displayReport() {
getLodnormal_list();
return "reportLordOfDataNormal.jsf?faces-redirect=true";
}
public List<Lodreport> getLodnormal_list() {
List<Lodreport> myLodreport = new ArrayList<Lodreport>(
lODBN.reportLODNormal(loginBean.getUsername()));
System.out.println("LOD list for Username :" + loginBean.getUsername() );
return myLodreport;
}
public List<Usergroupreport> getUsergroup_list() {
return usergroup_list;
}
public List<Usergroupdetail> getUsergroupdetail_list() {
List<Usergroupdetail> myUserGroupDetail = new ArrayList<Usergroupdetail>(
lODBN.listUserGroupDetail(loginBean.getUsername()));
System.out.println("username found is:" + loginBean.getUsername());
return myUserGroupDetail;
}
public String editLODDataNormal() {
lODBN.updateExistingLODDataNormal(security, loginBean.getUsername(),
access, authentifizierteBenutzer, auto, comment, comment1,
comment2, comment3, domain1, domain2, emailFeedback,
emailSendStatus, lOD1CostCenter, lOD1DisplayName, lOD1Domain,
lOD1Mail, lOD1User, lOD2CostCenter, lOD2DisplayName,
lOD2Domain, lOD2Mail, lOD2User, rechteGruppeChange,
rechteGruppeRead, type, rowId);
return "reportLordOfDataNormal.jsf?faces-redirect=true";
}
public List<Usergroupreport> getGroupId_list() {
List<Usergroupreport> myAllgroupIds = new ArrayList<Usergroupreport>(
lODBN.findAllGroupIdByUser(loginBean.getUsername()));
return myAllgroupIds;
}
public void edit(Lodreport myreport) {
this.myreport = myreport;
edit = true;
}
public void saveMyReport(){
lONRDB.updateReport(myreport);
}
}