I am new in spring and learning RedirectAttributes.Below is th sample code I am executing.
#RequestMapping(value="/")
public String app(Model model,RedirectAttributes rm) throws IOException{
UserCO userCO= new UserCO();
userCO.setEmail("rptdbay#gmail.com");
rm.addFlashAttribute("flashkey", "flashvalue");
rm.addFlashAttribute("userCO", userCO);
return "redirect:/controller1";
}
#RequestMapping(value="/controller1")
public String app1(Model model,HttpServletRequest request) throws IOException{
System.out.println("=====================================");
System.out.println("In Controller 1");
Map md = model.asMap();
for (Object modelKey : md.keySet()) {
Object modelValue = md.get(modelKey);
System.out.println("Model data =="+ modelKey + " -- " + modelValue);
}
java.util.Enumeration<String> reqEnum = request.getParameterNames();
while (reqEnum.hasMoreElements()) {
String s = reqEnum.nextElement();
System.out.println("Request data =="+ s+" : "+ request.getParameter(s));
}
return "redirect:/controller2";
}
#RequestMapping(value="/controller2")
public String app2(Model model,HttpServletRequest request) throws IOException{
System.out.println("=====================================");
System.out.println("In Controller 2");
Map md = model.asMap();
for (Object modelKey : md.keySet()) {
Object modelValue = md.get(modelKey);
System.out.println("Model data =="+ modelKey + " -- " + modelValue);
}
java.util.Enumeration<String> reqEnum = request.getParameterNames();
while (reqEnum.hasMoreElements()) {
String s = reqEnum.nextElement();
System.out.println("Request data =="+ s+" : "+ request.getParameter(s));
}
return "redirect:/controller3";
}
I have added String "flashvalue" and a object of bean userCO.I observed that-
Both "flashvalue" and userCO are available in Controller1.
Fine!,but in Controller2 "flashvalue" is available but userCO
not .Why so?
In Controller1 data is coming in Model ony but in Controller2 same
data is availavble in request only.Why so?
Below is my console log.
=====================================
In Controller 1
Model data ==userCO -- com.ttnd.mvc_mod.co.UserCO#60098260
Model data ==flashkey -- flashvalue
=====================================
In Controller 2
Request data ==flashkey : flashvalue
Is there any other way possible to get FlashAttribute rather than binding in Model?
First the explanation of your console log: You receive two model objects in controller 1. This is because you specify two flash attributes in method app. Flash attributes are stored in the HTTP session for the target controller, which retrieves it from there as model attributes. After that the flash attributes are erased by Spring.
In controller 2 the flash attributes are no longer available. The reason why “flashkey” is available in controller 2 as request attribute is, since primitive types in models are exposed as query parameters to the redirect target. As mentioned before, the flash attributes passed to controller 1 are exposed as model attributes. Your “userCO” is no primitive type, so you it will not get it as request parameter, while you will get “flashkey” as query parameter in controller 2.
If you want to switch off the “forward” of model objects as request parameters, set org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(boolean) to true (as suggested by Spring for ‘new’ applications).
I don’t know whether there is another way to get flash attributes. I’d encourage you to not work around the concepts of Spring here but handle the flash attributes just like you did in your question. Think of flash attributes as model attributes in your redirect target. If you – for what reason – need two redirect until you reach your target, add the desired attributes a second time as flash attributes in controller 1.
See http://docs.spring.io/spring/docs/4.1.7.RELEASE/spring-framework-reference/html/mvc.html#mvc-ann-redirect-attributes and http://docs.spring.io/spring/docs/4.1.7.RELEASE/spring-framework-reference/html/mvc.html#mvc-flash-attributes for any details.
Related
I'm trying to fetch value from db using JPA repository method
product findByIdNumberOrCifNumber(String idNumber , String cifNumber);
service class logic:-
public ResponseModel FindByCivIDOrCifNumber(String idNumber,String cifNumber) {
ResponseModel responseModel = new ResponseModel();
Optional<product> civId = Optional.ofNullable(productRepos.findByIdNumber(idNumber));
if (civId.isPresent()) {
responseModel.setResponse(productRepos.findByIdNumberOrCifNumber(idNumber,cifNumber));
} else {
errorModel errorModel1 = new errorModel();
enter image description here errorModel1.setErrorCode(productConstant.INVALID_REQUEST);
errorModel1.setErrorDescription("Requested Civil Id or CifNUmber is not present");
responseModel.setErrorModel(errorModel1);
}
return responseModel;
}
controller class:-
#GetMapping("/getByCifNoOrGetByIdNo")
public ResponseModel getProductByCifNoOrGetByIdNo(#RequestParam String idNumber,#RequestParam String cifNumber ) {
return productService.FindByCivIDOrCifNumber(idNumber,cifNumber);
}
post man:-
kindly help me out how to make it work:)
If you are looking for an answer to pass two or more path variables and test it with postman, you can try this.
#GetMapping("/api/mapping-name/{variable1}/{variable2}")
Here you will be getting two path variables which you can access by the syntax
#PathVariable("variable1") datatype variableName
Now in postman request url you can simply give the respective url, lets say:
https://localhost8080/api/mapping-name/:variable1/:variable2
which automaticaly will give you a key value section in the path variables section in the params with prepopulated key names as per the name you have given. In this case variable1 & variable2.
Give the respective value and it should work.
I am using MapStruct to convert a Page object to a custom object of my application. I am using this mapping in order to convert the content field of the Page object to a list of custom objects found in my data model:
#Mapping(target = "journeys", source = "content")
While this works OK and does convert the elements when content is present, this does not work correctly in case of no Page content. Taking a look at the code seems to show that the following check is added in the generated mapper class:
if ( page.hasContent() ) {
List<JourneyDateViewResponseDto> list = page.getContent();
journeyDateViewPageResponseDto.setJourneys( new ArrayList<JourneyDateViewResponseDto>( list ) );
}
When this is added the mapping action of the inner objects is omitted, meaning that I end up with a null list. I am not really sure as to why and how this check is added but I would like to find a way of disabling it and simply end up with an empty list of elements. Is there a way this can be done using MapStruct?
MapStruct has the concept of presence checkers (methods that have the pattern hasXXX). This is used to decide if a source property needs to be mapped.
In case you want to have a default value in your object I would suggest making sure that your object is instantiated with an empty collection or provide an #ObjectFactory for your object in which you are going to set the empty collection.
e.g.
Default value in class
public class JourneyDateViewPageResponseDto {
protected List<JourneyDateViewResponseDto> journeys = new ArrayList<>();
//...
}
Using #ObjectFactory
#Mapper
public interface MyMapper {
JourneyDateViewPageResponseDto map(Page< JourneyDateViewResponseDto> page);
#ObjectFactory
default JourneyDateViewPageResponseDto createDto() {
JourneyDateViewPageResponseDto dto = new JourneyDateViewPageResponseDto();
dto.setJourneys(new ArrayList<>());
return dto;
}
}
#Mapping(target = "journeys", source = "content", defaultExpression = "java(java.util.List.of())")
#RequestMapping(value = "/**", consumes = MediaType.ALL_VALUE)
#ResponseBody
public Object mirror(HttpServletRequest req, #Nullable #RequestBody Map<String, String> form) {
...
}
I just want the plain key and value for all form data entries here but it also includes query parameters in the map.
I need to be able to tell the difference between what came from the form and what came from the query.
Getting the query parameters separately is easily done using URI parsing but it's not so easy to remove the query parameters from the form map. Especially in the case they have the same keys.
Changing the parameter to MultiValueMap adds values with the same key into an array. Using just a Map causes the query parameters to overwrite the form data with equal keys.
I found where this is happening, for the MockHttpServletRequest at least: buildRequest method:
String query = this.url.getRawQuery();
if (!this.queryParams.isEmpty()) {
String s = UriComponentsBuilder.newInstance().queryParams(this.queryParams).build().encode().getQuery();
query = StringUtils.isEmpty(query) ? s : query + "&" + s;
}
if (query != null) {
request.setQueryString(query);
}
addRequestParams(request, UriComponentsBuilder.fromUri(this.url).build().getQueryParams());
this.parameters.forEach((name, values) -> {
for (String value : values) {
request.addParameter(name, value);
}
});
It's combining the form data and query data into one map. So is there an alternative way to parse the form data ONLY or exclude query params from the map!?
From the javadoc for #RequestParam:
In Spring MVC, "request parameters" map to query parameters, form data, and parts in multipart requests. This is because the Servlet API combines query parameters and form data into a single map called "parameters", and that includes automatic parsing of the request body.
Not sure if there's a more elegant way, but you could possibly use Spring's UriComponentsBuilder class to parse the URI string and get back the query parameters.
I know how to retrieve a bean from a service in a datafetcher:
public class MyDataFetcher implements DataFetcher {
...
#Override
public Object get(DataFetchingEnvironment environment) {
return myService.getData();
}
}
But schemas with nested lists should use a BatchedExecutionStrategy and create batched DataFetchers with get() methods annotated #Batched (see graphql-java doc).
But where do I put my getData() call then?
///// Where to put this code?
List list = myService.getData();
/////
public class MyDataFetcher implements DataFetcher {
#Batched
public Object get(DataFetchingEnvironment environment) {
return list.get(environment.getIndex()); // where to get the index?
}
}
WARNING: The original BatchedExecutionStrategy has been deprecated and will get removed. The current preferred solution is the Data Loader library. Also, the entire execution engine is getting replaced in the future, and the new one will again support batching "natively". You can already use the new engine and the new BatchedExecutionStrategy (both in nextgen packages) but they have limited support for instrumentations. The answer below applies equally to both the legacy and the nextgen execution engine.
Look at it like this. Normal DataFetcherss receive a single object as source (DataFetchingEnvironment#getSource) and return a single object as a result. For example, if you had a query like:
{
user (name: "John") {
company {
revenue
}
}
Your company resolver (fetcher) would get a User object as source, and would be expected to somehow return a Company based on that e.g.
User owner = (User) environment.getSource();
Company company = companyService.findByOwner(owner);
return company;
Now, in the exact same scenario, if your DataFetcher was batched, and you used BatchedExecutionStrategy, instead of receiving a User and returning a Company, you'd receive a List<User> and would return a List<Company> instead.
E.g.
List<User> owners = (List<User>) environment.getSource();
List<Company> companies = companyService.findByOwners(owners);
return companies;
Notice that this means your underlying logic must have a way to fetch multiple things at once, otherwise it wouldn't be batched. So your myService.getData call would need to change, unless it can already fetch data for multiple source object in one go.
Also notice that batched resolution makes sense in nested queries only, as the top level resolver can already fetch a list of object, without the need for batching.
I wonder how I can programmaticly set rows in an ExtendedDataTable as selected from my backing bean. I need to edit a user in my web app. The user has some roles so what I want is that when the page is loaded the groups which the user has are selected in the extendedDataTable.
I'm using Spring3 with JSF 2 and richfaces 4.
I think I need to bind the table to a backing bean which is in request scope. Can I use the Spring request scope for that? After that I need to implement the walk() on the datatable I guess. I have no idea where to go from there, can somebody point me in the right direction or give me an example?
Regards,
Derk
Here a piece of my code. This works, I see the "rowdata equals object" log statement but now I need to say to the row "selected" but there isn't a method for that as far as I know... How can I accomplish this?
public void selectRows(){
Collection<Object> s = new ArrayList<Object>(getGroups());
log.debug("set the selection to the table");
table.getTable().walk(FacesContext.getCurrentInstance(), new DataVisitor() {
#Override
public DataVisitResult process(FacesContext context, Object rowKey,
Object argument) {
log.debug("entered walk");
Collection<Object> selection = (Collection<Object>) argument;
for(Object o : selection){
table.getTable().setRowKey(rowKey);
if(table.getTable().getRowData().equals(o)){
log.debug("rowdata equals object");
table.getTable().getSelection().add(o);
log.debug("size of selection is: " + table.getTable().getSelection().size());
}
}
table.getTable().setRowKey(rowKey);
return null;
}
}, s );
}