What best way to pass #PathVariable to my all JSP pages without add it to ModelAndView? [closed] - java

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 8 years ago.
Improve this question
I change my path from:
/user/home
/user/history
...
to
/{orgId}/home
/{orgId}/history
...
So for all /{orgId}/* pages I need orgId on my JSP page to construct right links. How to do it without to get #PathVariable in each method and pass it to ModelAndView.
Thanks!

Use ControllerAdvice and ModelAttribute
#ControllerAdvice
class Advice {
#ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("orgId", "value1");
}
}
As of Spring 4, #ControllerAdvice can be customized through annotations(), basePackageClasses(), basePackages() methods to select a subset of controllers.

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.

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);

how to use a post method with a spring data rest process? [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
I'm using a repostiroy rest resource service and currently I need to implement a method to import the data from a excel file into my database. I don't see how I can do with spring repository rest resource? since it's an interface. do I use restController or does it have a way to do both?
The standard flow would be:
Create a repository file
#Repository
public interface RepositoryClass extends JpaRepository<T, ID>{}
where T is the class and ID is the data type of the ID (ex: Long, Integer etc.)
The JpaRepository already have save() and saveAll() methods(1)
In service create a method that read the data and saves it in the database
#Service
public class ServiceClass{
// ...some code
public void importData(// ... parameters){
// open the excel file and import the data in an ArrayList for exemple
repositoryClass.saveAll(arrayWithData);
}
}
The last step is to create a endpoint in the controller which calls the method that is written in service.
That interface have a method called save(). You can save the given object in database like: repository.save(myOjbect)

SpringBoot - CMS structure [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 5 years ago.
Improve this question
I am implementing a Spring Boot application that will be a micro-service.
This micro-service is supposed to define a CRUD #RestController over a CMS.
Is there any standard way of doing this, or any library I can take advantage of?
i.e:
#RequestMapping(value = "/simple/{key}", method = RequestMethod.GET)
public String getKey(#PathVariable final String key) {
return getCmsKey(key);//Retrieve key
}
#RequestMapping(value = "/collection/{key}", method = RequestMethod.GET)
public List<String> getKeys(#PathVariable final String key) {
return getCmsListOfKeys(keys);//Retrieve collection of keys
}

Java Api Login Issue [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 8 years ago.
Improve this question
I am creating a API to allow end users to update my database.
I will put the API classes in a jar file and provide the jar to the user.
However I do want that the user should be authorised.(provide username and password)
Once the user has provided the user name and password do I have to check if user is logged in in every API call user is making?(I mean do I have to check in every getter and setter if user is logged in or not)?
Is their a different way?May be checking if user login credentials are correct or not?if not then destroying the object created?
EDIT
API which will be in jar file
import package org.atul.module.registration;
public class HelloWorld(){
private bool isLogin=false;
private String userName="user";
private String Password="password";
public HelloWorld(String userName,String password){
isLogin = checkAuthentication(userName,password); //will make true if creadential are correct
}
public String getInformation(){
//return Information from database
}
//other getters and setters
As I cant stop user from creating an object Do I have to check isLogin is true in every function(getter and seter) I make?
I will give you a solution, may be is not the best but can help you. The intent is that you only can create objects with factories, so there you ask for user and pass. If you want you can make a cache of users logged in the superfactory to check if is it logged or not. I made you a simple diagram
create objects with the factory, register the user, throws an exception if doesn't exist pass and user.
In interface you declare methods that you want client use.
Constructor access level for classes is package. So your clients can't access them only with factories.
Another thing you can implements is like an Interceptor each request client gives you, pass to a class that check if is it logged the user.

Categories