In my application using JSF1.2 I have two jsp pages one is allemployee.jsp in which I placed a <h:dataTable> with 100 rows . Each contains employee's name,phone No,experience and so on... and the second page is
detail.jsp where I am displaying single employee's detail. In each row of allemployee.jsp there have a <h:commandLink> with value employee's name. After clicking this <h:commandLink> it displays corresponding
employee's detail in detail.jsp . Now in detail.jsp I have a <h:commandButton> named Go Back . I want after clicking this button user will come into the previous position of allemployee.jsp from where he/she
jumped to detail.jsp.
My allemployee.jsp looks like :
<h:form>
<h:dataTable id="employeeList" value="#{employeelist.employees}" var="employee" cellspacing="60">
<f:facet name="header"><h:outputText value="#{msg.list}" /></f:facet>
<h:column>
<f:facet name="header"><h:outputText value="#{msg.name}"/></f:facet>
<h:commandLink value="#{employee.name}" action="#{employeelist.getaction}" actionListener="#{employeelist.sendToDetail}">
<f:param id="NamE" value="#{employee.name}" name="NamE"/>
</h:commandLink>
</h:column>
<h:column>
<f:facet name="header"><h:outputText value="#{msg.phn}" /></f:facet>
<h:outputText value="#{employee.phNo}" />
</h:column>
<h:column>
<f:facet name="header"><h:outputText value="#{msg.experience}" /></f:facet>
<h:outputText value="#{employee.experiance}" />
</h:column>
</h:dataTable>
</h:form>
My detail.jsp looks like :
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="#{msg.name}" />
<h:outputText value="#{detail.employee.name}" />
<h:outputLabel value="#{msg.phn}" />
<h:outputText value="#{detail.employee.phNo}" />
<h:outputLabel value="#{msg.experience}" />
<h:outputText value="#{detail.employee.experiance}" />
</h:panelGrid>
<h:outputLink value="/allemployee.jsp#paramName">
<f:param name="paramName" value="#{detail.employee.name}"/>
<h:outputText value="Go Back" />
</h:outputLink>
employeelist Bean looks like :
public class EmployeeListController implements Serializable {
private static final long serialVersionUID = 6930161072142091260L;
private List<EmployeeModel> employees;
#PostConstruct
public void init() {
Service service = new Service();
this.employees = service.getAll();
}
public void sendToDetail(ActionEvent ev) {
String name = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NamE");
for (EmployeeModel employee : employees) {
if (employee.getName().equals(name)) {
HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
req.setAttribute("employe", employee);
}
}
}
public String goback(){
return "allemployee";
}
public String getaction() {
return "detail";
}
//getter & setter of employees.
}
In class EmployeeModel there are variables name,phNo ,experience and getters & setters.
In class Service I set all employees details.
My detail Bean is looks like :
public class DetailController implements Serializable {
private static final long serialVersionUID = 8632585974603579268L;
private EmployeeModel employee = new EmployeeModel();
private String paramName;
public DetailController() {
}
#PostConstruct
public void init() {
HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
this.employee = (EmployeeModel) req.getAttribute("employe");
this.paramName = employee.getName();
}
// getter & setter employee and paramName.
}
faces-config is like :
<managed-bean>
<managed-bean-name>detail</managed-bean-name>
<managed-bean-class>com.edfx.controller.DetailController</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>paramName</property-name>
<value>#{param.paramName}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>employeelist</managed-bean-name>
<managed-bean-class>com.edfx.controller.EmployeeListController</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>allemployee.jsp</display-name>
<from-view-id>/allemployee.jsp</from-view-id>
<navigation-case>
<from-outcome>detail</from-outcome>
<to-view-id>/detail.jsp</to-view-id>
</navigation-case>
</navigation-rule>
How can I fulfill my requirement? Any pointer will be very helpful to me. Thanks,
Related
I am having an app where the user picks his or her language:
I have two resources files, both of which work because I can see the text showing up in Xaml.
A short overview:
Eng Resource:
<data name="Next" xml:space="preserve">
<value>next</value>
</data>
<data name="German" xml:space="preserve">
<value>German</value>
</data>
<data name="Arabic" xml:space="preserve">
<value>Arabic</value>
</data>
The Labels:
<Label Text="{x:Static resources:AppResources.German}"
VerticalOptions="Center"
Grid.Row="0"/>
<Label Text="{x:Static resources:AppResources.Arabic}"
VerticalOptions="Center"
Grid.Row="1"/>
<Label Text="{x:Static resources:AppResources.English}"
VerticalOptions="Center"
Grid.Row="2"/>
I know that they work, becaause when I change from one emulator to another (one in germany, one in england) the language changes automatically.
But I want the user to be able to change the language upon button click.
I tried:
case "english":
CultureInfo culture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = culture;
mainPage.FlowDirection = FlowDirection.LeftToRight;
break;
But the phone simply ignores the language change.
Any ideas why?
You can create a LocalizationResourceManager class that handles language changes and also will have a property with the translation. When the language changes it will invalidate that string so it will force the property to change.
public class LocalizationResourceManager : INotifyPropertyChanged
{
public static LocalizationResourceManager Instance { get; } = new LocalizationResourceManager();
public string this[string text]
{
get
{
return AppResources.ResourceManager.GetString(text, AppResources.Culture);
}
}
public void SetCulture(CultureInfo language)
{
Thread.CurrentThread.CurrentUICulture = language;
AppResources.Culture = language;
Invalidate();
}
public event PropertyChangedEventHandler PropertyChanged;
public void Invalidate()
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
}
}
Instead of binding our strings directly to the AppResources class, a Translate Extension that returns a BindingProperty is used and binds to our new LocalizationResourceManager.
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension<BindingBase>
{
public string Text { get; set; }
public string StringFormat { get; set; }
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return ProvideValue(serviceProvider);
}
public BindingBase ProvideValue(IServiceProvider serviceProvider)
{
var binding = new Binding
{
Mode = BindingMode.OneWay,
Path = $"[{Text}]",
Source = LocalizationResourceManager.Instance,
StringFormat= StringFormat
};
return binding;
}
}
Usage:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MultilingualXFSample.Views.ChangeLanguagePage"
xmlns:helpers="clr-namespace:MultilingualXFSample.Helpers">
<Label Text="{helpers:Translate SelectLanguage}" />
</ContentPage>
For more, you can check: Mastering Multilingual in Xamarin.Forms
And there is a sample here: https://github.com/CrossGeeks/MasteringMultilingualSample .
I have a situation similar to the followa:
public class Shop {
#NotNull
String name;
#NotNull
String desc;
}
a button to show a form where I have to insert my user:
<form:form method="post" action="saveShop.html" modelAttribute="Shop">
<form:label path="name">Name:</form:label>
<form:input path="name" value="${shop.name}" />
<form:label path="desc">desc:</form:label>
<form:input path="desc" value="${shop.desc}" />
<input type="submit" value="Submit"/>
</form:form>
Controller:
#RequestMapping("/addShop")
public ModelAndView LoadFormPage(#ModelAttribute("Shop")Shop shop) {
ModelAndView model = new ModelAndView();
model.setViewName("/shop/addShop");
return model;
}
#RequestMapping(value = "/saveShop", method = RequestMethod.POST)
public ModelAndView saveShop(#Valid #ModelAttribute("Shop") Shop shop, BindingResult result) {
if (result.hasErrors()) { *do some*
} else { *do someelse* }
}
Even if I leave all fields blank (I have tried even with different data type) and submit the form the controller never recognize errors.
Can you help me?
Make sure to add below configs.
<bean id="myBeansValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
and
<mvc:annotation-driven validator="myBeansValidator">
and
<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
Adapted from Spring MVC form validation not working
Versions :
Aapche MyFaces 2.1.14
RichFaces 4.3.5
Issue :
I have a class implementing map interface like below :
public class FormStore implements Map {
private Map values;
public Object get(Object key) {
return values.get(key);
}
public Object put(Object key, Object value) {
return values.put(key, value);
}
}
I am using this map to store all submitted form values in my application and accessing in facelet as shown in below facelet code :
<h:inputText id="phone" value="#{formStore['phone']}" size="12" maxlength="20" required="true">
where formStore is the java class above.
Now I have added another Map in above java class as below which serves some special purpose.
public class FormStore implements Map {
private Map values;
private Map additionalValues;
public Object get(Object key) {
return values.get(key);
}
public Object put(Object key, Object value) {
return values.put(key, value);
}
//getter and setter method for additionalValues also added
}
The issue is I am not able to access this new map from EL . I have tried following options :
1)<h:inputText id="phone" value="#{formStore.additionalValues['phone']}" size="12" maxlength="20" required="true">
2)<h:inputText id="phone" value="#{formStore.[additionalValues]['phone']}" size="12" maxlength="20" required="true">
3)<h:inputText id="phone" value="#{formStore[additionalValues]['phone']}" size="12" maxlength="20" required="true">
For every option , it calls FormStore.get method with key = additionalValues
Is it not possible to access the additionalValues map from FormStore.java class ?
Please help.
This should work
<h:inputText id="phone" value="#{formStore.getAdditionalValues()['phone']}" size="12" maxlength="20" required="true">
I has a jsp page (index.jsp) with a form with two text fileds username and password like.
<form action="MyClass">
<input type="text" name="username" id="username" />
<input type="password" name="password" id="password" />
<input type="submit" />
</form>
On form submition i am invocking a servlet. I know that we can get the entered username and password values by using request methods,
request.getParameter("username");
request.getParameter("password");
But i don't want to use them , instead i want to store these values in a bean called BeanClass and i want to retrieve values from the bean in the sevlet. How can i get it??
You have to use <jsp:useBean/> action to instantiate the BeanClass with request or session scope in JSP.
Sample - EmpServlet.java
package com.me;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class EmpServlet extends HttpServlet {
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw=response.getWriter();
Emp emp=(Emp)request.getAttribute("emp");
pw.print(emp);
}
}
Emp.java : Emp bean
package com.me;
public class Emp {
private int age;
private String name;
public Emp() {
name="";
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean valid()
{
return age!=0 && name.length()!=0;
}
#Override
public String toString() {
return "Emp{" + "age=" + age + ", name=" + name + '}';
}
}
emp.jsp (view)
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:useBean id="emp" class="com.me.Emp" scope="request">
<jsp:setProperty name="emp" property="*"/>
</jsp:useBean>
<c:if test="${emp.valid()}">
<jsp:forward page="emp"/>
</c:if>
<form method="post" action="emp.jsp">
<br/><input type="text" name="age"/>
<br/><input type="text" name="name"/>
<br/><input type="submit"/>
</form>
web.xml
<servlet>
<servlet-name>EmpServlet</servlet-name>
<servlet-class>com.me.EmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmpServlet</servlet-name>
<url-pattern>/emp</url-pattern>
</servlet-mapping>
To the point, you're looking for a MVC framework like JSF or Spring MVC. With JSF it'll look something like this:
<h:form>
<h:inputText value="#{bean.username}" required="true" />
<h:inputSecret value="#{bean.password}" required="true" />
<h:commandButton value="submit" action="#{bean.submit}" />
<h:messages />
</h:form>
with
#ManagedBean
#RequestScoped
public class Bean {
private String username;
private String password;
public void submit() {
// Do here your job.
}
// Add/generate getters and setters.
}
That's all. No need for a servlet.
If you really want to do it the low level servlet way, you'd need to populate the bean yourself. This can be convenienced with Apache Commons BeanUtils to save boilerplate code.
Bean bean = new Bean();
BeanUtils.populate(bean, request.getParameterMap());
request.setAttribute("bean", bean);
// ...
The <jsp:useBean> does not allow for the MVC approach, it's more a MV. You have to mingle the conversion/validation into model and control the request/response inside the view, tasks which a controller should do. MVC frameworks offer you a controller which takes all this nasty boilerplate tasks from your hands.
Newbie Microsoft Unity question.
How do I specify the config to initialize a simple string type.
I'm thinking of the below... but it doesn't like the char[] paramater in the constructor.
<register type="string" name="myString" >
<constructor>
<param name="char[]" value="SomeValue">
</param>
</constructor>
</register>
Why don't inject the URL into the object using it, instead of leaving it to call Resolve?
public interface IMyService {
void DoSomething();
}
public class MyServiceRemote : IMyService {
private readonly _url;
public MyServiceRemote(string url) {
_url = url;
}
public void DoSomething() {
...
}
}
and, in the config:
<register type="IMyService" mapTo="MyServiceRemote" >
<constructor>
<param name="url" value="SomeValue">
</param>
</constructor>
</register>