I have this issue.
I redirect the user to /login page after registration, but I want to pass attribute from end-state of webflow to login.html that says something like "you have registered successfully". How can I accomplish that?
Edit: By the way I use thymeleaf.
when User clicks on Signup button the method which handles the registration there you need to add Model and insert the message as the attribute and pass to the login page.
example:
//Suppose these is the method which handles the registeration
void register(Model model)
{
model.addAttribute("Message","You are successFully Login");
return "loginPage";
}
And in login.jsp you can get the Message object as:
${Message}
Related
Version :
Apache MyFaces 2.1.14
Rich Faces 4.3.5
Issue :
I want to invalidate JSF session when user clicks on Logout page.
I have decided to use PreRenderViewEvent for the same.
Below is the design for the same :
When logout page is being rendered , call a listener method in back end bean through PreRenderViewEvent.
Invalidate JSF session in java method like : FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
My question is : is this a right approach in invalidating session ? Or is there any better approach available ?
Code :
<!-- call this in Logout.xhtml page -->
<f:event listener="#{bean.invalidateSession}" type="preRenderView" />
EDIT 1:
Let me elaborate the context of the question.
I have two web applications in my system.
When a user clicks on Logout button it always goes to webApp1.
The requirement is to invalidate session in webApp1 and then redirect to webApp2 where again invalidate the session.
The preRenderView event will be used when webapp1 LogOut page is being rendered. The listener there will invalidate the session and redirect to webapp2 LogOut page like below code :
public void logOut(ComponentSystemEvent event) throws IOException{
//logoutAction param will be read from request
String redirectUrl = "/webapp2/Logout.xhtml?action="+logoutAction;
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
FacesContext.getCurrentInstance().getExternalContext().redirect(redirectUrl);
}
I usually just add a button to my template :
<h:commandLink action="#{loginBean.logout()}" value="Logout" rendered="#{!empty loginBean.account}" />
This button calls my "login" manager bean, which contains an attribute Account account, populated with the user's parameters. It's also only rendered if the user is connected.
public String logout()
{
setAccount(null);
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
NavigationUtils.redirect("/admin/login");
return "";
}
I'm building this web app that works with user profiles. A user edit a profile and if he enters wrong data, the profile shows the invalid fields in red and shows the message. Very simple scenario. But for some reasons it doesn't work the way I assumed. I might be missing something somewhere. Here is what I do in my controller.
As you can see in the code below, I create a new Profile, I bind the params to it using an include list to only bind those params that profile accepts, and then call hasErrors on the profile instance. In debug mode, I can see that myProfile has errors when the user enters something wrong, but when I get back to the editProfile page, it doesn't show what field has the error. my gsp form has the render error tag and also hasError for each field.
def update (){
def myProfile = new Profile()
dataBind(params, myProfile, include:[.....])
if (myProfile.hasErrors()){
respond myUser.errors, view: 'editProfile'
return
}
else{
redirect(action:"viewProfile")
}
}
My gsp is something like this, I don't have access to my home computer right now to post the exact gsp page:
<g:hasErrors bean="${myUser}" field="name">
<g:message error="${myUser.errors.getFieldErrors("name")}" />
</g:hasErrors>
Instead of trying to pass the errors back, pass the entire object to your gsp. Assuming the bean you have in your gsp page is 'myUser', then this is what the render statement should be:
render view: 'editProfile', model: [myUser: myProfile]
When you use respond bean.errors, the bean will be called based on class name, not variable name.
In your case, you need to access the bean in you gsp as profile like so:
//variable in controller
def myProfile = new Profile()
//bean in views
<g:hasErrors bean="${profile}" field="name">
<g:message error="${profile.errors.getFieldErrors("name")}" />
</g:hasErrors>
If you need to call your bean with a specific name like myUser, just pass it in the model:
render (view:'editProfile', model:[myUser: myProfile])
view has this anchor:
echo anchor('login', 'Login or Register');
how do i send the current url to my controller login? and then use it on another function called login_validation?
all i want is, login and back to the last url, however nothing works. so i thought saving the current url when i click "Login or Register" and then after login, on function login_validation, i should redirect to that url saved...
controller index
public function index(){
$this->main_login();
}
main_login
public function main_login(){
$this->load->helper('url');
// on view i will call the next function login_validation
$this->load->view("view_login");
}
login_validation
public function login_validation(){
$this->load->library('form_validation');
(...)
if ($this->form_validation->run()){
// i should redirect the saved url here, instead of home
redirect('home');
}else{
(...)
}
}
i appreciate any help
You can do this simply by using $this->agent->referrer() in your controller class main_login().
Save the referrer url into your session, and once the user is validated (or if they are), then you pull that string from session:
in main_login():
// grabs url where you clicked login
$this->session->set_userdata('referrer_url', $this->agent->referrer());
in login_validation():
// pull url from session and redirect, make sure to account for not having url too
redirect( $referrer_url );
I apologize for the delay, i was really busy with the end of the semester,
So i will explain how i did to solve this problem, all i wanted was back to the current page after the login.. so, everytime i open my view login i will save the last page url, with this code:
$refering_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '' ;
and then i should save it in my session, so i can access from my controller and redirect the user to the last page, but look, i can’t simple save the url every time i access the view login, beucase every time i miss the password, i will be redirected to the login page, and so the last url, will the login and not the last one, and of course its wrong, So we have to save it on session, but only in the first time we access the view login, to do that, i compare the variable above ($refering_url) to the view login and view login validation, if its not the same, i can confirm that the previous page is the one i was before the login, and then i can save it to my session,
here the comparison, and the last url saved in my session:
if (($refering_url != ‘URL TO VIEW LOGIN‘) &&
($refering_url != ‘URL TO LOGIN VALIDATION){
$this->session->set_userdata('url', $refering_url);
}
after login is validated, on the controller, i should redirect the user , to the last page he was (the url saved on the session), to do that i used this code:
$this->session->set_userdata($data);
$url=$this->session->userdata('url');
redirect($url, 'refresh');
I just try to prevent re submission problem when refresh form page. so I use session flashdata() method and redirect same form page. but I want also display recent inputed data on form VIEW page. then I am try $this->db->insert_id() on my form VIEW page . but it always show 0. how can I solve it?
when you redirect the user after the successful form submission then add the parameter in the url just a exapmle with dummy code to make an idea for you
$insertedid=$this->my_model->add_info();
//your add_info() should return the inserted id
redirect("/myformcontroller/myformpage/".$this->db->insert_id());// add id in redirect url
on your myformcontroller controller's myformpage function
function myformpage() {
$insertedid=$this->url->segment(3);
if(!empty($insertedid)){
//get new added information and pass it to view
}
// your other code
}
hope it makes sense
I have a few links (login, logout, and register) in the _layout template, where the links are shown depending on whether the user is logged in. Like so:
if (User.Identity.IsAuthenticated)
{
<span class="username">#User.Identity.Name</span>
<span class="link">#Html.ActionLink("Logout", "Logout", "Account")</span>
}
else
{
<span class="link">#Html.ActionLink("Login", "Login", "Account")</span>
<span class="link">#Html.ActionLink("Register", "Register", "Account")</span>
}
Problem is that the logout link is still displayed the first time the user logs out of the system (I would expect that to be immediately replaced with the login, and register links) - that is until the page is refreshed, or the user moves to another page. Here is the logout action code:
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Abandon();
return View();
}
I have gone through this link - http://mvcdev.com/differences-between-asp-net-razor-and-web-forms-view-engines/ - which explains the execution order of the Razor engine, but in my case it seems to be executing differently. Ideally I would expect the FormsAuthentication.SignOut() to execute before the User.Identity.IsAuthenticated in the _layout.
What am I doing wrong? Thanks!
That's normal, you need to redirect after logging out:
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Abandon();
return RedirectToAction("Index");
}
The reason this happens is because when the client requested the Logout link he was still authenticated (he sent the authentication cookie along with the request). Then inside the controller action you are logging him out (FormsAuthentication.SignOut()) which does nothing more than mark the authentication cookie for removal on subsequent requests. Then you return a view, and inside this view of course the user is still authenticated as this view executes under the same request and the cookie is still present.