My Applicatoin is based on Spring 3, Hibernate, JSP and MySQL DB
While registering user, I want to check if username exists in DB. What is the best way to achieve this? can someone please provide example or reference to other posts?
I have tried below stuff so far
On submit of a page, I have a service call in controller which returns user object (if username exists) otherwise null. i.e. "public User userService.getUser(userName)"
I dont know how to pass the message to jsp so posting this message
I am not sure if this is the correct approach? of not please suggest
better one
I'll make a ajax call to check the existence of user by calling the this function inside the controller, and display the appropriate message if the user already exist.
public boolean isUserExist(String userName)
{
return userDao.isExist(userName);
}
Related
So I'm new to Spring Boot and am trying to get my head around how it works. But I can't find some of the answers I'm looking for online so was hoping somebody might be able to help me out.
My first question is, can a web application use its own rest api to
manipulate data eg. get, post, put etc. or is the api just limited
to use by other applications/websites etc. If this is the case how does a a web
application manipulate it's data does it just use a seperate
conventional controller?
My second question is, let's say I've a piece of code like this
#GetMapping("/responsebody")
#ResponseBody
public UserAccount testingResponseBody(Principal principal) {
if(principal != null) {
UserAccount currentUser = userRepo.findByUserName(principal.getName());
return currentUser;
}else {
return null;
}
}
A simple piece of code that returns a JSON for the currentUser. The
thing that has me a little confused is why would someone want the
current user JSON to be visible at the corresponding URL i.e
localhost:8080/responsebody. I mean lets say the controller is accessed by an AJAX request. The data is only needed internally in the
application. Why display it to the world at that URL. I feel like I'm missing something important. Is there a way to make certain controller methods only usable within the application to manipulate data without showing it at a URL.
Also if anyone knows of any really good resources where I can get
these concepts to sink in it would be greatly appreciated.
Thanks guys, hope I didn't make it too long.
I think that if you expose path then every apps can access to that path. If you want to use internal, you can restrict that path using security for example like "only apps that have authority 'INTERNAL_CLIENT' should only be access to that path"
I'm making android app that uses Parse.com.
I have a subclass of ParseObject and one of the key (say for example "keyForPointingUser") of that object is some ParseUser.
I'm logged in as User a for example and in that particular parse object subclass I created pointing to user b. When calling
ParseObjectSubClass.get("keyForPointingUser").getUsername();
I get an exception
ParseObject has no data for this key...
though I see in Parse data in the web browser that my ParseSubclass has a Prase User in the "keyForPointingUser" key....
Any ideas?
Thank u in advance
a bit late to answer but i've faced what you had problem here and i solved it with including user class to query :
query.include("user");
Good day!
I used the Struts2 xml validation as instructed in this website.
The problem is when I clicked the submit button twice. The error message also appears twice...
My question is... how to clear the first error message before another action is processed to accommodate new set of error messages.
Thank you in advance.
If you are using the spring integration you have to define your bean as scope="prototype", then you get a new instance of your Action for every request.
The default scope for spring is singleton.
It's a good idea to do this for every Action.
Remove validate="true" from form tag.
There is a validator interceptor in the default stack. You just need to use that and using that is very simple. Just make a method in your action class by the name public void validate(). Within that validate() you can access the fields using their getters & then put the required validation onto them.
Also, with this implementation you would not have to worry about the multiple messages being shown, because it will show only the message what you set in the addFieldError method and removes any previously kept messages.
NOTE: Be sure to use getters of the variables in your validate(), because the variables in the action are not set at the time this interceptor is invoked.
Here is a link to a very nice tutorial.
http://www.vaannila.com/struts-2/struts-2-example/struts-2-validation-example-1.html
If you will keep validate=false, then validation xml will be called on submit action.
To clear messages on each submit click, if you are using table in your jsp then keep table outside of form. It will work.
We are using MVC3 with unobstructuve validation, all is fine; we got our custom validations working on the server as well as on the client.
We are also using the build-in asp.net Role provider. We have particular Views that we use for different Roles. Some of the fields (properties of the ViewModel) are Required based on the Role of the logged-in user.
My question is: How do we make a ValidationAttribute based on a Role? We want to make a property Required when the user is Normal User; but we don't want to make that field required when the user is an Administrator. But we do want to use the same view.
I know we can create a custom ValidationAttribute (creating a class that derives from ValidationAttribute) I suppose that will work fine for Server-Side validation. But we really want to validate this Client-Side.
I reckon we implement the IClientValidatable in our above ValidationAttribute and write some jQuery to make an Ajax call back to the server to check the current logged in user and its roles.
But this will make us do an Ajax call every time the specific property changes, and the logged-in user will not change.
What is the right way to do this? Or should we create different ViewModels for different logged in user?
Please let me know when my above explanation doesn't make sense, I can add some example code when needed.
Any help is appreciated.
Thanks,
Pim
I think you can indeed implement a custom ValidationAttribute that also implements the IClientValidatable. But then you could just "reuse" the unobtrusive required validation for the client side:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
if ( <your condition, e.g. if user has a certain role> )
yield return new ModelClientValidationRequiredRule(<error message>);
}
This should generate the same unobtrusive required attributes on the input as the RequiredAttribute, but only if your condition is met.
I'm using a modified version of Felix Geisendörfer's SimpleAuth/SimpleAcl components that I've combined into a single Component, Simple_Authable.
I changed his startup() function to initialize() to not clutter the beforeFilter function in my app_controller.
One of the things that this component does is check who the active user is and if that user can't be found it either looks him up based on the primary User.id or uses 'guest'. Either way, the component uses $this->Controller->Session->write() to save the active user or guest information.
I'm also using Felix's Authsome plugin instead of the default CakePHP Auth component.
When I'm logging in, the active user is guest, obviously.
After I've submitted the form, the active user is still guest because the component's initialize() function is firing before everything else. Then, the Authsome plugin comes into play and validates my user as "root" and also calls $this->SimpleAuthable->setActiveUser($id, true); to force SimpleAuthable to update the active user information it is storing via $this->Controller->Session; Then I am redirected and my simple Session information and DebugKit's Session tab reflect that I am indeed the root user.
However, when I try to navigate to an 'admin' page, let's say /admin/users/index, lo and behold SimpleAuthable thinks I'm still a 'guest' user because when it performs a $this->Controller->Session->read() call to the key holding my user id, it is getting an empty response, i.e., the data stored on the previous page didn't persist.
Maybe there is something funky happening between Authsome & SimpleAuthable, but things look pretty straightforward and to my mind, $this->Controller->Session should be saving and persisting the data written to it.
So, I'm looking at refactoring all the calls to $this->Controller->Session and replacing them with $this->Session but first I wanted to throw this out to the community and see if anybody has seen anything similar and if so how did they resolve it.
Sincerely,
Christopher.
I found the problem... I'm also using Joshua McNeese's Permissionable plugin and I needed to disable it for the $this->Controller->{$this->userModel}->findById($id); in my SimpleAuthable component when I try to lookup the current active user.
Note to self: I would have caught this faster if I had some unit testing in place :(.