How jsp can access getters and setters in superclass, Spring MVC [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Using spring MVC
I have some class with property, and getter\setter to get access to this property
class a{
private String something;
public void setSomething(String something){
this.something = something;}
public String getSomething(){
return something;
}
And I have subclass with some new property, getter and setter in it
class b extends a{
private String newProp;
public void setNewProp(String newProp){
this.newProp = newProp;}
public String getNewProp(){
return newProp;
}
When I trying to get property value in jsp, that defined in superclass like this
${b-inst.something}
I've got "is not readable or had invalid getter" error
Is it possible to get access to that superclass property without changing this property to protected, and writing it's getter\setter in subclass(because I'm losing inheritance benefits in that way)?

You should use correct case for the property: userName instead of username (just like your firstName and lastName properties)

Related

How to make one method return different values for user with role ADMIN and USER? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I need to make the #Get method, and for users with ROLE_USER it should return other values than for users with ROLE_ADMIN.
The URL have to be the same.
How to do it with Spring Security?
If your authentification passed well you could just inject #AuthenticationPrincipal:
#GetMapping("/get-url-here")
public String main(#AuthenticationPrincipal User user) {
if (user.getRole().equals("ROLE_ADMIN")) {
// set values for admin
} else {
// set for user
}
return "view-or-response-body";
}
I assume that you have the User class configured, like:
public class User {
public String role;
// other fields, getters, setters
}
Or role could be enum as well.
Question is not 100% clear, but what I would do is make an Enum field in the user class (assuming that class exists) for the role and return the value of that field when the URL is called.

Should client pull full object information or field only when required from REST? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Current I has the below server scenario
---------------------------------------------------------Redis Cache
Client <--(Low Latency/instantly)--> REST API <----+
---------------------------------------------------------Database
Dude to the low latency between client and api i decided to do the cache at api side
which also mean the client will not be caching
then I start to think about the question
Assume I have a Person class which contains name and money field that I have in my client
and the API support fetching all Person's information at once with api.com/person
or fetching Person's information one by one with api.com/person?fields=name and api.com/person?fields=money
should i request all of the information when the object is being created like this
Class Person {
String name;
int money;
public Person() {
JsonObject data = getJsonData(`api.com/person`);
this.name = data.get('name');
this.name = data.get('money');
}
public String getName() {
return name;
}
public int getMoney() {
return money;
}
}
in this case, if the client only require Name of Person, but the object also pull the money from
server which is wasted for traffic or etc.
or, i can replace the getName() function be like this
public String getName() {
JsonObject data = getJsonData(`api.com/person?fields=name`);
return data.get('name');
}
which does not waste any fields
but the downside is, when client require both Name and Money field
it would then double up the request required
I dont know which one is better for my case, any further explain are very welcome too.
You could use a builder pattern to build person object by building proper url for fetching one or more fields in a single api hit. e.g.
new PersonBuilder().addName().build(); // url attribute of PersonBuilder in this case should be "api.com/person?fields=name"
or
new PersonBuilder().addName().addMoney().build(); // url attribute of PersonBuilder in this case should be "api.com/person?fields=name,money"
in build() method, you could call different setter methods based on what you added using addName(), addMoney() by maintaining different booleans in PersonBuilder corresponding to each field e.g. nameAdded = true, moneyAdded =false.
This way you will never need to hit the api multiple times.

How to get Spring bean by name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I get the bean by passing only the class name using BeanFactoryUtils
I'm trying below, but this is not working
import org.springframework.beans.factory.BeanFactoryUtils;
baseDao= BeanFactoryUtils.originalBeanName("RegionDaoImpl");
RegionDao
#Component
public class RegionDaoImpl implements BaseDao<Region> {
...
}
Any suggestions?
You need a ListableBeanFactory, then you call beanOfType(), e.g.:
RegionDaoImpl dao = BeanFactoryUtils.beanOfType(beanFactory, RegionDaoImpl.class);
Generally, the ListableBeanFactory will be an ApplicationContext, so you need the application context of your Spring application. How to get that depends on your application type, and where the code calling beanOfType() is located.
It is usually better to let Spring auto-wire the object into your class, which does the same thing, i.e. lookup the bean by type.
#Component
public class SomeComponent {
#Autowire
private RegionDaoImpl regionDao;
...
}
If you want to lookup by name, you'd call beanFactory.getBean(), but that seems kind of redundant:
RegionDaoImpl dao = beanFactory.getBean("RegionDaoImpl", RegionDaoImpl.class);

Why is it impossible to inject generic classes? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a generic abstract template class. I thought if I create type-specific Producers, I could inject some DAO service directly in the generic class. But I can't.
Why? And how could I work around this?
abstract class MyView<T> {
#Inject
MyDao<T> dao;
//some more template methods that make use of the dao
void someMethod() {
dao.use();
}
}
class CustomerView extends MyView<Customer> {
//javax.enterprise.inject.AmbiguousResolutionException: Ambigious resolution
}
class DaoManager {
#Produces
MyDao<Customer> getDaoCustomer() {
return DaoFactory.make(Customer.class);
}
#Produces
MyDao<Product> getDaoProduct() {
return DaoFactory.make(Product.class);
}
}
When I inject eg a #Inject MyDao<Customer> dao; it works perfectly. But not with generics...
When you request
#Inject MyDao<Customer> dao;
the container knows that you want a bean specifically of type MyDao<Customer>. If such a bean exists and its type information is known, then the container can satisfy the injection. For example, the type information is preserved in your #Produces annotated method
#Produces
MyDao<Product> getDaoProduct() {
The container uses reflection to retrieve that parameterized type and can match it to the requested #Inject field.
With
abstract class MyView<T> {
#Inject
MyDao<T> dao;
however, all the container knows is that you want a MyDao. T is a type variable, not a concrete parameterization. The container cannot assume a specific type for it. In your case, both of the #Produces beans would match and there would be ambiguity.
In your example, we know from the context that it really wants a MyDao<Customer>. That doesn't seem to be something your container is capable of doing, ie. trying to resolve the type parameter to a concrete type argument for a parameterized subclass.

Looking for method argument validation framework based on AOP and annotations [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm looking for argument validation framework which:
1) Allows specifying argument constraints via annontations (like OVal, JaValid)
2) Validation code automatically injected (during compilation or runtime) into methods (i.e. no explicit call to Validator object is required)
Example of what i'm looking for:
public class Person {
private String name;
....
//Method which arguments should be validated
public void setName(#NotBlank String name){
//<---validating code should be injected here
this.name = name;
}
}
//Example of call to the validated method
...
Person person = new Person();
person.setName("John");
...
Example of code i'm trying to avoid
...
Validator validator = new Validator(...);//glue code
Person person = new Person();
person.setName("John");
validator.validate(person);//glue code
...
Thanks for answers!
I think you meant "automatically injected during compilation or runtime", right?
I had the same problem. My solution was Spring Validation and self-written AOP layer (about three classes).
My validation code looks like this:
#Validational( validators =
{"com.mycompany.MyValidator"} )
public void myMethod( String paramToValidate )

Categories