getting complete URI address in JSF [duplicate] - jsf

This question already has an answer here:
How to get the URL of current page in JSF?
(1 answer)
Closed 3 years ago.
How can I get a complete URI address ( http:// .../ ../.) in JSF using FacesContext ?

Should be something like this:
public String getRequestUrl()
{
Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
if (request instanceof HttpServletRequest)
{
String requestedUrl = ((HttpServletRequest) request).getRequestURL().toString();
return requestedUrl;
}
return "";
}
P.s., since you are new to StackOverflow, please vote my answer up and accept if it helped.

Related

Initialize method in javafx [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Can one controller class have two or more initialize () ?
Can multiple statements be in a single initialize() ?
#question 2...This is my controller class:
#Overide
public void initialize(URL url,ResourceBundle rb)
{
//adding itemList variable to itemBox
try{
itemBox.setValue("Gari");
itemBox.setItems(itemList);
}
catch(Exception e){
System.out.println(e);
}
//Animation for changing scene
String filename = url.getFile().substring(URL.getFile().lastIndexOf('/')+1, URL.getFile().length());
if(filename.equals("FXML.fxml")){
//calling fadeTransition method
fadeTrans(anchorPane);
}
else if(filename.equals("SignUp_In.fxml")){
fadeTrans (anchorPaneSignUp_In);
}
}
and it returns this error message:
java.lang.NullPointerException
Here, the multiple statements are adding itemList to itemBox and changing scene with animation
no
yes
Just some more characters to get over the minimum.

Uploading file in JSF (Need correct file pathway) [duplicate]

This question already has an answer here:
How to upload file using JSF 2.2 <h:inputFile>? Where is the saved File?
(1 answer)
Closed 5 years ago.
I am trying to get my JSF site to upload a picture to the server, but am having a time of it. I've found 4 methodologies to do, but I'd like to use h:InputFile as it seems the most direct.
It would seem I just need to supply the upload path correctly.
After adding #MultipartConfig I no longer get an exception, but I can't verify the file is uploaded or see any error.
public void AddPicture()
{
ConnInfo HitIt = new ConnInfo();
try
{
HitIt.save(fileCelebrityToAdd);
}
catch(Exception ex)
{
//?
}
}
#MultipartConfig(location="C:\\local\\pathway\\Netbeans\\project\\web\\Pictures\\items\\")
public class ConnInfo
{
private String uploadLocation;
public ConnInfo()
{
//uploadLocation = ".\\Pictures\\items\\";
uploadLocation = "C:\\local\\pathway\\Netbeans\\project\\web\\Pictures\\items\\";
}
public boolean TryOut(Part file) throws IOException
{
String monkey = uploadLocation+getFilename(file);
try
{
file.write(monkey);
}
catch(Exception ex)
{
return false;
}
return true;
}
}
Hopefully I've copied the necessary information correctly.
After going back and rereading all the articles I had bookmarked, it was actually the from the one Tam had suggested that I was able to strip out some information.
I didn't need the AJAX, or the #MultipartConfig, and my previous attempt was somehow incorrect, but the follow method allowed me to successfully upload a picture where I wanted it:
public boolean SaveHer(Part file)
{
String monkey = getFilename(file);
try (InputStream input = file.getInputStream())
{
Files.copy(input, new File(uploadLocation, monkey).toPath());
}
catch (IOException e)
{
// Show faces message?
return false;
}
return true;
}

Primefaces graphicImage stream not closed, file locked [duplicate]

This question already has answers here:
Display dynamic image from database or remote source with p:graphicImage and StreamedContent
(4 answers)
Closed 6 years ago.
I'm using primefaces to upload an image, crop it and then display the final image on a graphicImage.
The process works fine, but the problem is that when I retrieve the final image to display on the graphicImage, the stream is not closed and the file is being held up by java.exe, so I'm having problems on deleting the files/directory for example when the user logs out, because it's just a temp directory.
This is the getter of my StreamedContent:
public StreamedContent getGraphicCropped() {
try{
if (newImageName != null) {
File file2 = new File(pathCroppedImage);
InputStream input = new FileInputStream(file2);
graphicCropped = new DefaultStreamedContent(input);
showImageFinal = true;
}
} catch(Exception e){
e.printStackTrace();
}
return graphicCropped;
}
If I do input.close();then I'm able to delete the file, but it is not displayed, because I know that this getter is called more than once on the life cycle.
I've solved it by using the suggested getter of a StreamedContent:
public StreamedContent getGraphicCropped() throws FileNotFoundException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
return new DefaultStreamedContent();
}
else {
// So, browser is requesting the image. Return a real StreamedContent with the image bytes.
File file2 = new File(pathCroppedImage);
InputStream input = new FileInputStream(file2);
showImageFinal = true;
return new DefaultStreamedContent(input);
}
}

JSF, session timeout handling [duplicate]

This question already has answers here:
Authorization redirect on session expiration does not work on submitting a JSF form, page stays the same
(2 answers)
Closed 4 years ago.
I have configured my session timeout in the server and have added a filter to handle session timeout. But when I am trying to redirect it back to the login page its not working. I searched the net but not getting anything solid. I am using jsf.. my code
public class SessionTimeoutFilter implements Filter {
private String timeoutPage = "login.seam";
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain filterChain) throws IOException,ServletException {
if ((request instanceof HttpServletRequest)
&& (response instanceof HttpServletResponse))
{
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
if (isSessionControlRequiredForThisResource(httpServletRequest)) {
if (isSessionInvalid(httpServletRequest))
{
String timeoutUrl = httpServletRequest.getContextPath()
+ "/" + getTimeoutPage();
System.out.println("Session is invalid! redirecting to timeoutpage : " + timeoutUrl);
httpServletResponse.sendRedirect(timeoutUrl);
return;
}
}
}
filterChain.doFilter(request, response);
}
Can anyone tell me what am i doing wrong... why is sendredirect not responding
Maybe this solution will be proper for your needs:
How to redirect to index page if session time out happened in jsf application
if you need perform some action on session timeout you can also create #Destory annotated method on session statefull bean.

Passing the locale value in URL

I have a requirement where I need to display the same page in different language when the user selects a language from the dropdown.
For this I am using a selectOneMenu with multiple languages. When the user selects a language(locale) , the value should be appended to the url.
I have used the below code but it replaces the already present parameters from the url with locale.
Is there any way I can append the locale parameter without disturbing the parameters already present.
FacesContext ctx = FacesContext.getCurrentInstance();
String contxRoot = ctx.getExternalContext().getRequestContextPath();
String viewId = ctx.getViewRoot().getViewId();
String URL=viewId+"?language="+this.selectedLaguage;
try {
FacesContext.getCurrentInstance().getExternalContext().redirect(contxRoot+URL);
} catch (IOException e) {
e.printStackTrace();
}
Any help would be appreciated.
This code is not a beauty, but you could try
FacesContext ctx = FacesContext.getCurrentInstance();
String contxRoot = ctx.getExternalContext().getRequestContextPath();
String initialUri = ((HttpServletRequest) ctx.getExternalContext().getRequest()).getRequestURI();
String languageParameter = "?language="+this.selectedLaguage;
try {
FacesContext.getCurrentInstance().getExternalContext().redirect(contxRoot+initialUri+languageParameter);
} catch (IOException e) {
e.printStackTrace();
}
This sounds like a good candidate for storing in a #SessionScoped bean. Set the language in the bean and it can be used by all pages requested by the user (during the course of this session, of course).
A custom filter that intercepts a request and sets the language from the url and redirects appropriately can be option if, for example, a user wants to link to a URL in a particular language. Whether or not you actually want to allow that is up to you...

Resources