In my Spring web project, I have added a class object in to the model map. This is the class
public class ProjectDetailsBean {
private String title;
private String type;
private String addedBy;
private String status;
private String updatedOn;
private String relavantBranch;
// getters and setters are here
}
After setting all the attributes to a ProjectDetailsBean object instance, I add that to the model map in my controller class.
epb is that instance.
model.addAttribute("projectDetails", epb);
Now I need to access the fields of projectDetails in a jsp page. Those values need to be assigned to separate input fields. I cannot figure out how to do that. Could you please tell me how to do that.
Thank you!
You need to access through Expression language
${projectDetails.title}
${projectDetails.addedBy}
....... so on
Related
I have a 1:N relationship where a Victim might have lots of EmergencyContacts.
I've created a DTO called VictimDTO and inside of it there's a List and I'm using ModelMapper to convert my DTO into an Entity.
When I get rid of the list just for testing purposes, it works fine placing this code snippet inside of the insert "POST" method in the Controller layer in Java:
VictimEntity victimEntity = modelMapper.map(victimDTO, VictimEntity.class);
But now I've placed this List inside of my VictimDTO and I'm getting an Exception when trying to use this code.
My DTO actual code is this one as it follows below:
public class VictimDTO {
private String name;
private int age;
private String email;
private String phone;
private List<ContactDTO> contactDTOList;
//getters and setters
}
How can I use ModelMapper to convert a POJO with a Collection (List) as an instance variable inside into an Entity?
Thank you for your help.
Best regards,
Lucas AbrĂ£o
I have a question regarding Thymeleaf and Spring Boot. I'm creating a form wizard and I would like to have the same object passed between multiple controllers, so that the object (SimpleCharacter) stores each time the value from each page.
What I have right now is, that with each endpoint, I get a new object created that "forgets" what I wanted to store from the previous page. How can I achieve that to have the same instance of the object passed between endpoints and in the end fully completed object with fields from each previous endpoint?
private static final String CHARACTER = "character";
#GetMapping(value = "/new-char/01_race")
public String showCharWizRace(Model model) {
CharacterDto character = new SimpleCharacter();
model.addAttribute(CHARACTER, character);
return "new-char/01_race";
}
#PostMapping(value = "/new-char/02_class")
public String showCharWizClass(Model model, #ModelAttribute CharacterDto character) {
model.addAttribute(CHARACTER, character);
model.addAttribute("classes", charClassService.findAll());
return "new-char/02_class";
}
#PostMapping(value = "/new-char/03_attributes")
public String showCharWizAttributes(Model model, #ModelAttribute CharacterDto character) {
model.addAttribute(CHARACTER, character);
return "new-char/03_attributes";
}
Thank you very much for all valuable hints and help. I've searched the Web, but couldn't find anything useful to point me in the right direction.
EDIT: But if you make CharacterDto have more fields for example race, class, attributes and use each time only one page (one form) to provide one field, spring "forgets" the other property when opening the next form. For example: 1st page: race is set, 2nd page (no race field existing here) class is set but in this place the previously set race had been already forgotten.
CharacterDto fields, that should be filled step by step on each page:
private String race;
private String charClass;
private int strength;
private int endurance;
private int dexterity;
private int charisma;
private int intelligence;
private int perception;
private String name;
private String surname;
private String description;
private String title;
private String avatar;
First, your character field are inside a spring form?
If yes, you also could to store your variable in a hidden field and pass this by #RequestParam.
Follow a example:
<input th:field="*{character}" name="character"/>
And in your controller method add a request parameter variable
showCharWizClass(#RequestParam(value = "character", required = false) String character, otherVariables){}
If it doesn't work, you also try to use something like a template strategy with session.
Putting your variable in a session scope, changing the variable with each request and removing it on last access.
Here a good link about access data from templates:
https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html
UPDATE
You need to combine Model and Session Attributes in your workflow pages.
In your controller add a SessionAttribute pointing to the DTO that is using, like this:
#Controller
#SessionAttributes("character")
public class WizardController { ..
And when you have finished your flow, you can end session attributes this way.
#GetMapping(value = "/new-char/04_clear")
public String clearSession(SessionStatus sessionStatus) {
sessionStatus.setComplete();
return "new-char/04_clear";
}
If you look at my example code I add a new page to clean session and restart a form with a default DTO values.
Working on a REST client that calls another server which returns the following object:
public class ObjectOriginal {
private int id;
private String name;
// constructor/getters/setters
}
I need to obfuscate the id. To do so I'm using an already existing service that transforms the id into a unique generated String so that the person calling my service doesn't know the REAL id but can still request info about it with the unique string.
So I'm basically trying to return to the caller this object:
public class ObjectNew {
private String id;
private String name;
// constructor/getters/setters
}
Do I need to have a copy of ObjectOriginalDTO + create a ObjectNew DTO + create a mapper to go from one to the other.
Or can I configure Jackson to deserialize the id field as a String and not an int?
You can do this using your own Serializer/Deserializer.
You have to implement your Serializer/Deserializer that will extends respectively BeanSerializerModifier/BeanDeserializerModifier and configuring your Module with them for instance Or use the annotation base solution as explained in this tutorial, there are plenty of references on the web for such a thing. then you'll have more controlle over the way to map your id.
If you don't want to have custom deserializer you can have:
public class ObjectNewDto {
private String id;
private String name;
// constructor/getters/setters
}
and another object:
public class ObjectOriginal {
private int id;
private String name;
// construxtor/getters/settes
}
Now after validating ObjectNewDto you can map it via your obfuscator service into ObjectOriginal , then validate this Object original and so on...
I need to serialize a pojo into different json structure depending on whom I am sending request. Also I should be able to configure in some config that how field of pojo are mapped to json properties for a given request.
Can this be achived using jackson?
Is there some library or api to do this?
Edit:
For example:
public class Universal {
private int id;
private Date date;
private String name;
private Inner inner;
private Map<String,Object> others;
private List<Inner> inners;
}
public class Inner {
private String value;
}
now above are two object i need to create dynamic json, one example for some of transformation is below
{
"id":"",//value will be id of Universal
"detials":{
"name":"",//value will be name of Universal
},
"data":[], // array of value(field of Inner) from inners
"ext":{
"prop1":""// value of this field will be some (key1) value from others
}
}
You can use Google Gson and rely on its type adaptors.
http://www.javacreed.com/gson-typeadapter-example/ is a good article from web
Hy guys, I've turning around a problem using Struts2.
Basically I've a form that the user can submit filling different fields. I would like to populate conditionally the right object with the properties insert by the user.
So for example, if the user fill the form in a intermediary way I would like to use the properties for to create an Intermediary object, instead if the user fill the request in entity way, i'll use the properties for to create the Entity object, ecc...
These object share the same interface Request.
Is there any way to use polimorphically properties for to create Intermediary or Entity object?
Suppose this is my two POJO:
public class Intermediary implements Request{
private String name;
private String surname;
private String code;
private String FiscalCode;
private String address;
...
/*Getters and setters */
....
}
public class Entity implements Request{
private String name;
private String surname;
private String code;
....
/*Getters and setters */
....
}
This could be my form into the jsp page:
<s:form action="fillRequest" id="formRichiesta" name="formRichiesta" method="post">
<s:radio id="tipoRichiedente" name="tipoRichiedente" list="richiedenti">
<label>Name</label><s:textfield name="name"/>
<label>Surname</label><s:textfield name="surname"/>
<label>code</label><s:textfield name="code"/>
<label>Adress</label><s:textfield name="adress"/>
<label>Fiscal Code</label><s:textfield name="fiscalCode"/>
</s:form>
<s:submit>
Basically my problem is that I can discover the kind of request the the user trying to fill only ofter submission and watching at the radiobutton state.
Is there any way to mapping the properties into the right object directly instead of creating one big object with all the form properties and then create the right implementation? for to map properties could I use the interface reference?
For example image my action:
public class RequestAction {
private Request request;
....
}
So in my jsp could I use:
<label>Name</label><s:textfield name="request.name"/>
Thanks in advance.