Cannot process submitted foreign keys - spring

There are 3 one-to-many relationships into an entity Pta :
#Entity
#Table(name = "pta")
public class Pta {
#Id()
#SequenceGenerator(name="s_pta", sequenceName="pta.s_pta", allocationSize=1)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s_pta")
#Column(name="pta_code")
private Integer code;
#Column(name="pta_intitule")
private String lib;
#ManyToOne
#JoinColumn(name = "obj_code")
private Objectif objectif;
#ManyToOne
#JoinColumn(name = "struct_code")
private Structure structure;
#ManyToOne
#JoinColumn(name = "exer_code")
private Exer exercice;
public Pta() {
super();
}
public Pta(Integer code) {
super();
this.code = code;
}
// getters and setters
}
#Entity
#Table(name = "objectif")
public class Objectif {
#Id
#SequenceGenerator(name="s_objectif", sequenceName="pta.s_objectif", allocationSize=1)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s_objectif")
#Column(name = "obj_code")
private int code;
#Column(name="obj_intitule")
private String lib;
#ManyToOne
#JoinColumn(name = "prog_code")
private Programme programme;
#ManyToOne
#JoinColumn(name = "nat_obj_code")
private NatureObjectif nature_objectif;
public Objectif() {
super();
}
public Objectif(int code) {
super();
this.code = code;
}
public Objectif(int code, String lib) {
super();
this.code = code;
this.lib = lib;
}
// getters and setters
}
#Entity
#Table(name = "structure")
public class Structure {
#Id()
#Column(name="struct_code")
private String code;
#Column(name="struct_sigle")
private String sigle;
#Column(name="struct_lib")
private String lib;
public Structure() {
super();
}
public Structure(String code) {
super();
this.code = code;
}
// getters and setters
}
#Entity
#Table(name = "exercice")
public class Exer {
#Id()
#Column(name="exer_code")
private String exerCode;
#Column(name="exer_lib")
private String exerLibelle;
public Exer(){
super();
}
public Exer(String exer_code) {
super();
this.exerCode = exer_code;
}
// getters and setters
}
I want to insert a record into the Pta.
controller for page jsp :
modelView.addObject("exercices",exerDao.list());
modelView.addObject("structures",structureDao.list());
HashMap<String, String> criteres = new HashMap<String, String>();
criteres.put("nat_obj_code", "DdP");
modelView.addObject("docs_perf", objectifDao.lireParCritere(criteres));
criteres.clear();
criteres.put("nat_obj_code", "ODD");
modelView.addObject("odds", objectifDao.lireParCritere(criteres));
criteres.clear();
criteres.put("nat_obj_code", "PRO");
modelView.addObject("produits", objectifDao.lireParCritere(criteres));
modelView.addObject("action", request.getContextPath().concat("/elaboration/savePtaEtDetails"));
modelView.addObject("pta_formulaire", new Pta());
return modelView;
jsp :
<form:form cssClass="form-horizontal" servletRelativeAction="${action}" method="post" commandName="pta_formulaire">
<form:hidden path="code"/>
<div class="row">
<div class="col-sm-8">
<div class="form-group">
<label class="col-sm-4 control-label">Exercice</label>
<div class="col-sm-4">
<form:select path="exercice" cssClass="validate[required]">
<form:option value="" label=" -- Sélectionner -- "/>
<form:options items="${exercices}" itemValue="exerCode" itemLabel="exerLibelle" />
</form:select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Document de performance</label>
<div class="col-sm-4">
<form:select path="objectif" style="width:500px;" cssClass="validate[required]">
<form:option value="" label=" -- Sélectionner -- "/>
<form:options items="${docs_perf}" itemValue="code" itemLabel="lib" />
</form:select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">ODD</label>
<div class="col-sm-4">
<select id="odd" name="odd" style="width:500px;">
<option value=""> -- Sélectionner -- </option>
<c:forEach items="${odds}" var="odd">
<option value="${odd.code}">${odd.lib}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Axes du PND</label>
<div class="col-sm-4">
<select id="pnd" name="pnd" style="width:500px;" multiple></select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Programmes</label>
<div class="col-sm-4">
<select id="programme" name="programme" style="width:500px;" multiple></select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Effets</label>
<div class="col-sm-4">
<select id="effet" name="effet" style="width:500px;" multiple></select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Produits PMO</label>
<div class="col-sm-4">
<select id="produit" name="produit" style="width:500px;" multiple>
<option value=""> -- Sélectionner -- </option>
<c:forEach items="${produits}" var="produit">
<option value="${produit.code}">${produit.lib}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Actions PMO</label>
<div class="col-sm-4">
<select id="action" name="action" style="width:500px;" multiple></select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Activité</label>
<div class="col-sm-4">
<select id="activite" name="activite" style="width:500px;">
<option value=""> -- Sélectionner -- </option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Résultat attendu</label>
<div class="col-sm-4">
<select id="resultat" name="resultat" style="width:500px;">
<option value=""> -- Sélectionner -- </option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Indicateur</label>
<div class="col-sm-8">
<select id="indicateur" name="indicateur" style="width:500px;">
<option value=""> -- Sélectionner -- </option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<fieldset>
<legend><label class="control-label">Programmation physique</label></legend>
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<label class="col-sm-1 control-label">T1</label>
<div class="col-xs-10">
<input class="form-control" type="text" name="t1">
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="col-sm-1 control-label">T2</label>
<div class="col-xs-10">
<input class="form-control" type="text" name="t2">
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="col-sm-1 control-label">T3</label>
<div class="col-xs-10">
<input class="form-control" type="text" name="t3">
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="col-sm-1 control-label">T4</label>
<div class="col-xs-10">
<input class="form-control" type="text" name="t4">
</div>
</div>
</div>
</div>
</fieldset>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<fieldset>
<legend><label class="control-label">Programmation financière</label></legend>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label class="col-sm-1 control-label">Montant</label>
<div class="col-xs-3">
<input class="form-control" type="text" name="progr_financiere">
</div>
</div>
</div>
</div>
</fieldset>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label class="col-sm-4 control-label">Structure</label>
<div class="col-sm-4">
<form:select path="structure" style="width:230px;" cssClass="validate[required]">
<form:option value="" label=" -- Sélectionner -- "/>
<form:options items="${structures}" itemValue="code" itemLabel="lib" />
</form:select>
</div>
</div>
</div>
<input class="btn btn-primary btn-sm" type="submit"/>
</form:form>
In the controller target :
#RequestMapping(value = "/savePtaEtDetails", method = RequestMethod.POST)
public ModelAndView savePtaEtDetails(#ModelAttribute("pta_formulaire") Pta pta, #RequestParam String exercice, #RequestParam int objectif,
#RequestParam String structure) {
System.out.println("eeeeeeeeeeeeeeee");
return new ModelAndView("redirect:/accueil");
}
At runtime when submitting the form then I got the error The request sent by the client was syntactically incorrect. So what is wrong ?

I found it :) The solution was to not set the attribute in the constructor of the "master" entities :
public Exer(String exer_code) {
super();
}
public Structure(String code) {
super();
}
public Objectif(String code) {
super();
}

Related

org.springframework.beans.NotReadablePropertyException:Invalid property

Invalid property 'stCode' of bean class [java.lang.String]: Bean property 'stCode' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
Regist.jsp
<div class="container">
<div class="row centered-form">
<div class="col-xs-12 col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-heading">
</div>
<div class="panel-body">
<f:form action="registSave" modelAttribute="r" method="post">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
State Name
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<f:input path="stCode" class="form-control input-sm"/>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
District Name
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<f:input path="distCode" class="form-control input-sm"/>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
Phone number
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<f:input path="phone" class="form-control input-sm"/>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
Name
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<f:input path="name" class="form-control input-sm"/>
</div>
</div>
</div>
<!-- <div class="form-group">
<input type="email" name="email" id="email" class="form-control input-sm" placeholder="Email Address">
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password" id="password" class="form-control input-sm" placeholder="Password">
</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<input type="password" name="password_confirmation" id="password_confirmation" class="form-control input-sm" placeholder="Confirm Password">
</div>
</div>
</div> -->
<input type="submit" value="Register" class="btn btn-info btn-block">
</f:form>
</div>
</div>
</div>
</div>
</div>
RBean
package max.Bean;
public class RBean {
String stCode;
String distCode;
String phone;
String name;
public String getStCode() {
return stCode;
}
public void setStCode(String stCode) {
this.stCode = stCode;
}
public String getDistCode() {
return distCode;
}
public void setDistCode(String distCode) {
this.distCode = distCode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Controller
package max;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import max.Bean.RBean;
#Controller
public class MainController {
#Autowired
RBean rBean;
#RequestMapping("home")
public String home()
{
return "registLayout";
}
#RequestMapping("regist")
public ModelAndView registration()
{
return new ModelAndView("reg","r","rBean");
}
#RequestMapping(value="registSave", method=RequestMethod.POST)
public ModelAndView registrationSave(#ModelAttribute("r") RBean rBean)
{
return new ModelAndView("reg","r","rBean");
}
}
if you are trying to set rBean into r variable which is available in view then you should remove the " used for rBean. Eg: return new ModelAndView("reg","r",rBean);

picture not displaying laravel 8

This is my code
AdminAddProductComponent.php
<?php
namespace App\Http\Livewire\Admin;
use App\Models\Category;
use Livewire\Component;
use Illuminate\Support\Str;
use App\Models\Product;
use Livewire\WithFileUploads;
use Carbon\Carbon;
class AdminAddProductComponent extends Component
{
use WithFileUploads;
public $name;
public $slug;
public $short_description;
public $description;
public $regular_price;
public $sale_price;
public $SKU;
public $stock_status;
public $featured;
public $quantity;
public $image;
public $category_id;
public function mount()
{
$this->stock_status = 'instock';
$this->featured = 0;
}
public function generateSlug()
{
$this->slug = Str::slug($this->name,'-');
}
public function addProduct()
{
$product = new Product();
$product->name = $this->name;
$product->slug = $this->slug;
$product->short_description = $this->short_description;
$product->description = $this->description;
$product->regular_price = $this->regular_price;
$product->sale_price = $this->sale_price;
$product->SKU = $this->SKU;
$product->stock_status = $this->stock_status;
$product->featured = $this->featured;
$product->quantity = $this->quantity;
$imageName = Carbon::now()->timestamp.'.'.$this->image->extension();
$this->image->storeAs('products',$imageName);
$product->image = $imageName;
$product->category_id = $this->category_id;
$product->save();
session()->flash('message','Product has been created successfully!');
}
public function render()
{
$categories=Category::all();
return view('livewire.admin.admin-add-product-component',['categories'=>$categories])->layout('layouts.base');
}
}
admin-add-product-component.blade.php
<div>
<div class="container"style="padding:30px 0;">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<div class="row">
<div class="col-md-6">
Add New Product
</div>
<div class="col-md-6">
All Products
</div>
</div>
</div>
<div class="panel-body">
#if(Session::has('message'))
<div class="alert alert-success" role="alert">{{Session::get('message')}}</div>
#endif
<form class="form-horizontal" enctype="multipart/form-data"wire:submit.prevent="addProduct">
<div class="form-group">
<label class="col-md-4 control-label">Product Name</label>
<div class="col-md-4">
<input type="text" placeholder="Product Name" class="form-control input-md"wire:model="name"wire:keyup="generateSlug"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Product Slug</label>
<div class="col-md-4">
<input type="text" placeholder="Product Slug" class="form-control input-md"wire:model="slug"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Short Description</label>
<div class="col-md-4">
<textarea class="form-control" placeholder="Short Description"wire:model="short_description"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Description</label>
<div class="col-md-4">
<textarea class="form-control" placeholder="Description"wire:model="description"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Regular Price</label>
<div class="col-md-4">
<input type="text" placeholder="Regular Price" class="form-control input-md"wire:model="regular_price"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Sale Price</label>
<div class="col-md-4">
<input type="text" placeholder="Sale Price" class="form-control input-md"wire:model="sale_price"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">SKU</label>
<div class="col-md-4">
<input type="text" placeholder="SKU" class="form-control input-md"wire:model="SKU"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Stock</label>
<div class="col-md-4">
<select class="form-control"wire:model="stock_status">
<option value="instock">InStock</option>
<option value="outofstock">Out of Stock</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Featured</label>
<div class="col-md-4">
<select class="form-control"wire:model="featured">
<option value="0">No</option>
<option value="1">Yes</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Quantity</label>
<div class="col-md-4">
<input type="text" placeholder="Quantity" class="form-control input-md"wire:model="quantity"/>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Product image</label>
<div class="col-md-4">
<input type="file" class="input-file"wire:model="image"/>
#if($image)
<img src="{{$image->temporaryUrl()}}"width="120"/>
#endif
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Category</label>
<div class="col-md-4">
<select class="form-control"wire:model="category_id">
<option value="">Select Category</option>
#foreach ($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
I don't know why it's not working.. I mean the picture cant display on my Image show product page...
https://www.youtube.com/watch?v=IcGAM05UMwA&list=PLz_YkiqIHesvPtvLl2Wz5FtuW44dBt199&index=18&ab_channel=SurfsideMedia I follow this video<<< it just I dont know why my picture not showing out. it show the alt="picture" things
add use Livewire\Carbon; instead of usu carbo

How to do server side validation in spring mvc using validator class

Hi am new to spring mvc i have to do sever side validation using separate validator class. i have two model with one to many relation i have to register them before register them i am new server side validation in spring mvc to use spring form and all
<form id="studentEnrollmentForm" method="post" class="form-horizontal" action="saveStudentByAdmin">
<div class="form-group">
<label class="col-xs-2 control-label">Student Full Name</label>
<div class="group">
<div class="col-xs-3">
<input type="text" class="form-control" name="studentFirstName" id="fn" placeholder="First name" />
</div>
</div>
<div class="col-xs-3">
<input type="text" class="form-control" name="studentMiddleName" placeholder="Middle name" />
</div>
<div class="col-xs-3">
<input type="text" class="form-control" name="studentLastName" placeholder="Last name" />
</div>
</div>
<div class="form-group">
<label class="col-xs-2 control-label">Parents Full Name</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="parentFirstName" placeholder="First name" />
</div>
<div class="col-xs-3">
<input type="text" class="form-control" name="parentMiddleName" placeholder="Middle name" />
</div>
<div class="col-xs-3">
<input type="text" class="form-control" name="parentLastName" placeholder="Last name" />
</div>
</div>
<div class="form-group">
<label class="col-xs-2 control-label">Date-of-birth</label>
<div class="col-xs-3 ">
<div class="input-group input-append date" id="studentDOB">
<input type="Text" class="form-control" name="studentDOB" /> <span
class="input-group-addon add-on"><span
class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
<label class="col-xs-3 control-label">Gender</label>
<div class="col-xs-3">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default">
<input type="radio" name="studentGender" value="male" />Male</label>
<label class="btn btn-default">
<input type="radio" name="studentGender" value="female" />Female</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-xs-2 control-label">Phone</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="parentPhoneNumber" placeholder="Phone number" />
</div>
<label class="col-xs-3 control-label">Email</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="parentEmail" placeholder="Email" />
</div>
</div>
<div class="form-group">
<label class="col-xs-2 control-label">Permanent Address</label>
<div class="col-xs-3">
<textarea class="form-control" rows="3" name="studentPermanentAddress" /></textarea>
</div>
<label class="col-xs-3 control-label">Present Address</label>
<div class="col-xs-3">
<textarea class="form-control" rows="3" name="studentPresentAddress" /></textarea>
</div>
</div>
<div class="form-group">
<label class="col-xs-2 control-label">Class to join</label>
<div class="col-xs-3">
<!-- <input type="text" class="form-control" name="className" placeholder="Enter Class" /> -->
<select name="className" class="form-control">
<option value="">Select class </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-xs-1 ">
<button type="submit" class="btn btn-primary ">Submit</button>
</div>
<div class="col-xs-1 ">
<button type="reset" class="btn btn-default" id="rstbutton">Refresh</button>
</div>
</div>
</form>
Controller
#Controller
#RequestMapping("/")
public class AdminRegistrationController {
#Autowired
private IAdminRegistrationService adminRegistrationService;
#RequestMapping(value = "/register", method = RequestMethod.GET)
public String viewRegistrationPage(Model model) {
StudentDTO studentDTO = new StudentDTO();
ParentDTO pdto=new ParentDTO();
model.addAttribute("teacherDTO", studentDTO);
model.addAttribute("teacherDTO", pdto);
return "StudentEnrollmentByAdmin";
}
#RequestMapping(value = "/saveStudentByAdmin", method = RequestMethod.POST)
public String saveTeacherByAdmin(#ModelAttribute StudentDTO sdto,#ModelAttribute ParentDTO pdto) {
System.out.println(sdto.getStudentFirstName());
System.out.println(pdto.getParentFirstName());
return "redirect:/register"; // this line redirecting to above method to avoid same data insertion again when i press f5
//return "TeacherEnrollmentByAdmin"; To know duplication insertion comment above line and above method current this line
//when you get response page(after insertion of data) press f5 and see in data base
}
}
My Example validator class
#Component
public class StudentRegistrationFromAdminValidator implements Validator{
public boolean supports(Class<?> clazz) {
return StudentDTO.class.isAssignableFrom(clazz);
//return ParentDTO.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
StudentDTO student = (StudentDTO)target;
ParentDTO parent = (ParentDTO)target;
String studentFirstName = student.getStudentFirstName();
String parentFirstName=parent.getParentFirstName();
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "studentFirstName", "student.studentFirstName.empty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "parentFirstName", "parent.parentFirstName.empty");
if(studentFirstName.equals("pradee")){
errors.rejectValue("studentFirstName", "student.studentFirstName.invalid");
}
if(parentFirstName.equals("pradee")){
errors.rejectValue("parentFirstName", "parent.parentFirstName.invalid");
}
}
}
In StudentDTO and ParentDTO classes, you can give various validator annotations from javax.validation.constraints package and org.hibernate.validator.constraints package(if using hibernate).
public class StudentDTO {
#Size(min=6, max=12)
private String firstName;
#Size(min=6, max=12)
private String lastName;
//getter setter
}
Then add javax.validation.Valid annotation and
org.springframework.validation.BindingResult interface in controller method as follows.
#RequestMapping(value = "/saveStudentByAdmin", method = RequestMethod.POST)
public String saveTeacherByAdmin(#Valid #ModelAttribute StudentDTO sdto,#ModelAttribute ParentDTO pdto,
BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
System.out.println(bindingResult.getAllErrors().get(0).getDefaultMessage());
}
}

Value is not coming in back end in spring Hibernate

Hi all i am trying to create one page which have relationship between client and Discharge. I have define one combo box which populate data of client form model and i want that client id in Discharge table but i am not getting value in back end if you inspect it in front end its look good but its null in back end so pls help me to solve this problem.
Here the code,
DischargeCard.java
#Entity
#Table(name = "Discharge_card")
public class DischargeCard {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private int DischargeId;
#ManyToOne(cascade = {CascadeType.ALL})
#JoinColumn(name = "Client_id")
private Client Client_id;
#Column(name = "IPDNo")
private String ipdno;
#Column(name = "DOA")
private Date DOA;
#Column(name = "DOO")
private Date DOO;
#Column(name = "DOD")
private Date DOD;
#Column(name = "DOB")
private Date DOB;
#Column(name = "Age")
private int age;
#Column(name = "Sex")
private String sex;
#Column(name = "Phone")
private String phone;
#Column(name = "Address")
private String Address;
#Column(name = "Diagnosis")
private String diagnosis;
#Column(name = "Treatment")
private String treatment;
#Column(name = "Anesthesia")
private String anesthesia;
#Column(name = "Clinical_Presentation")
private String clinical_presentation;
#Column(name = "Investigation")
private String investigation;
#Column(name = "Given_Treatment")
private String given_treatment;
#Column(name = "Advice_on_Discharge")
private String advice;
< generates setter / getter >
Client.java
#Entity
#Table(name = "CLIENT")
public class Client {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
int clientId;
#NotEmpty(message = "Please Enter Client Name..." )
#Column(name = "Name")
String clientName;
#Column(name = "ContactName")
String contactName;
#NotEmpty(message = " Please Enter Client Address...")
#Column(name = "Address")
String clientAddress;
#NotEmpty(message = "Please Enter Client Phone...")
#Size (min = 10)
#Pattern (regexp = "[0-9]*")
#Column(name = "Phone")
String phone;
#Email (message = "Please Enter Proper Email Address of Client...")
#Column(name = "Email")
String email;
#NotEmpty (message = "Please Enter Client City...")
#Column(name = "City")
String city;
#NotEmpty (message = "Please Enter Client Zip Code...")
#Size (min = 6)
#Pattern (regexp = "[0-9]*")
#Column(name = "Zip")
String zip;
#NotEmpty(message = "Please Enter Client State...")
#Column(name = "State")
String state;
#NotEmpty (message = "Please Enter Client Country...")
#Column(name = "Country")
String country;
#Column(name = "Private")
String privateNote;
#Column(name = "Other")
String otherNote;
#ManyToOne
#JoinColumn(name = "Company_id")
private Company company;
#OneToMany(mappedBy = "Client_id")
private Set<Invoice> invoice;
#OneToMany(mappedBy = "Client_ID")
private Set<Estimate> estimate;
#OneToMany(mappedBy = "Client_id")
private Set<DischargeCard> discharges;
< generated setter / getter >
DischargeCardController.java
#Controller
public class DischargeCardController {
#Autowired
DischargeCardService ds;
#Autowired
ClientService service;
#RequestMapping(value = "/dishcharge", method = RequestMethod.GET)
public String showClientDetailsPage(Model m, HttpServletRequest req) {
m.addAttribute("dischargecard", new DischargeCard());
m.addAttribute("actionURL", "saveCard");
List<Client> obj = service.getAll("");
m.addAttribute("clientList", obj);
List<String> clientlist = new ArrayList<>();
for(Client c : obj)
{
clientlist.add(c.getClientName().toString());
}
m.addAttribute("client_name_list",clientlist);
return "dischargeCard";
}
#RequestMapping(value = "/saveCard", method = RequestMethod.POST)
public String saveCompanyDetails(#ModelAttribute("DischargeCard")DischargeCard dc, BindingResult result,
HttpServletRequest req) {
String redirectTo = "";
/*if(result.hasErrors())
{
redirectTo = "dischargeCard";
return redirectTo;
}
else
{*/
ds.saveData(dc);
redirectTo = "redirect:dishcharge";
return redirectTo;
}
#RequestMapping(value = "/searchDischargeCard", method = RequestMethod.GET)
public ModelAndView showUsersPage() {
ModelAndView mv = new ModelAndView("searchDischargeCard");
List<Client> obj = service.getAll("");
mv.addObject("clientList", obj);
List<String> clientlist = new ArrayList<>();
for(Client c : obj)
{
clientlist.add(c.getClientName().toString());
}
mv.addObject("client_name_list",clientlist);
mv.addObject("DischargeList",ds.getAll(""));
return mv;
}
}
DischargeCard.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<jsp:include page="header.jsp"></jsp:include>
<jsp:include page="menu.jsp"></jsp:include>
<!-- BEGIN Content -->
<div id="main-content">
<!-- BEGIN Page Title -->
<div class="page-title">
<div>
<h1>
<i class="fa fa-file-o"></i> Add Dishcharge Card
</h1>
<h4>Add data of new Discharge Card</h4>
</div>
</div>
<!-- END Page Title -->
<!-- BEGIN Breadcrumb -->
<div id="breadcrumbs">
<ul class="breadcrumb">
<li><i class="fa fa-home"></i> Home <span
class="divider"><i class="fa fa-angle-right"></i></span></li>
<li class="active">Discharge</li>
</ul>
</div>
<!-- END Breadcrumb -->
<div class="row">
<div class="col-md-12">
<div class="box box-pink">
<div class="box-title">
<h3>
<i class="fa fa-bars"></i> Discharge Card
</h3>
<div class="box-tool">
<a data-action="collapse" href="#"><i class="fa fa-chevron-up"></i></a>
<a data-action="close" href="#"><i class="fa fa-times"></i></a>
</div>
</div>
<div class="box-content">
<%-- <form action="#" > --%>
<form:form action="${actionURL}" class="form-horizontal"
id="wizard-validation" method="post"
modelAttribute="dischargecard">
<form:hidden path="DischargeId" id="DischargeId" />
<div class="form-wizard" id="form-wizard-2">
<ul class="row steps steps-fill">
<li class="col-xs-12 col-sm-6 col-lg-3"><a href="#tab2-1"
data-toggle="tab" class="step active"> <span class="number">1</span>
<span class="desc"><i class="fa fa-check"></i> Client
Details</span>
</a></li>
<li class="col-xs-12 col-sm-6 col-lg-3"><a href="#tab2-2"
data-toggle="tab" class="step"> <span class="number">2</span>
<span class="desc"><i class="fa fa-check"></i>
Treatment Details</span>
</a></li>
<li class="col-xs-12 col-sm-6 col-lg-3"><a href="#tab2-3"
data-toggle="tab" class="step"> <span class="number">3</span>
<span class="desc"><i class="fa fa-check"></i> Photo's</span>
</a></li>
<li class="col-xs-12 col-sm-6 col-lg-3"><a href="#tab2-4"
data-toggle="tab" class="step"> <span class="number">4</span>
<span class="desc"><i class="fa fa-check"></i> Confirm</span>
</a></li>
</ul>
<div class="progress progress-striped">
<div class="progress-bar progress-bar-primary"></div>
</div>
<div class="tab-content">
<div class="tab-pane active" id="tab2-1">
<div class="form-group">
<label for="firstname2"
class="col-sm-3 col-lg-2 control-label">Client Name</label>
<div class="col-sm-5 col-lg-3 controls">
<!-- <input type="text" name="firstname2" id="firstname2"
class="form-control" data-rule-required="true"> -->
<%-- <form:select path="Client_id"
class="form-control chosen" data-rule-required="true"
style="border-radius: 8px;"
data-placeholder="Choose a client" tabindex="1">
<option value=""></option>
<c:forEach var="client" items="${clientList}">
<form:option value="${client.clientId}">${client.clientName}</form:option>
</c:forEach>
</form:select> --%>
<form:select path="Client_id" items="${clientList}"
itemValue="clientId" itemLabel="clientName" />
</div>
</div>
<div class="form-group">
<label for="firstname2"
class="col-sm-3 col-lg-2 control-label">IPD Number</label>
<div class="col-sm-5 col-lg-3 controls">
<form:input path="ipdno" name="ipdno" type="text"
class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Date of
Admission</label>
<div class="col-sm-5 col-lg-3 controls">
<form:input path="DOA" name="doa"
class="form-control date-picker" id="dp1" size="16"
type="text" value="" />
</div>
<div class="input-group col-sm-5 col-lg-2">
<a class="input-group-addon" href="#"><i
class="fa fa-clock-o"></i></a>
<form:input path="DOA" name="doa"
class="form-control timepicker-default" type="text" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Date of
Operation</label>
<div class="col-sm-5 col-lg-3 controls">
<form:input path="DOO" name="doo"
class="form-control date-picker" id="dp1" size="16"
type="text" value="" />
</div>
<div class="input-group col-sm-5 col-lg-2">
<a class="input-group-addon" href="#"><i
class="fa fa-clock-o"></i></a>
<form:input path="DOO" name="doo"
class="form-control timepicker-default" type="text" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Date of
Discharge</label>
<div class="col-sm-5 col-lg-3 controls">
<form:input path="DOD" name="dod"
class="form-control date-picker" id="dp1" size="16"
type="text" value="" />
</div>
<div class="input-group col-sm-5 col-lg-2">
<a class="input-group-addon" href="#"><i
class="fa fa-clock-o"></i></a>
<form:input path="DOD" name="dod"
class="form-control timepicker-default" type="text" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Date of
Birth</label>
<div class="col-sm-5 col-lg-3 controls">
<form:input path="DOB" name="dob"
class="form-control date-picker" id="dp1" size="16"
type="text" value="" />
</div>
</div>
<div class="form-group">
<label for="lastname2" class="col-sm-3 col-lg-2 control-label">Age</label>
<div class="col-sm-5 col-lg-3 controls">
<form:input path="age" name="Age" type="text" id="lastname2"
class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Radio
Buttons</label>
<div class="col-sm-9 col-lg-10 controls">
<label class="radio-inline"> <form:radiobutton
path="sex" name="optionsRadios2" value="Male" /> Male
</label> <label class="radio-inline"> <form:radiobutton
path="sex" name="optionsRadios2" value="Female" /> Female
</label>
</div>
</div>
<div class="form-group">
<label for="firstname2"
class="col-sm-3 col-lg-2 control-label">Phone</label>
<div class="col-sm-5 col-lg-3 controls">
<form:input path="phone" name="Phone" type="text"
class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Address</label>
<div class="col-sm-5 col-lg-5 controls">
<form:textarea path="Address" name="address"
class="form-control" rows="4"></form:textarea>
</div>
</div>
</div>
<div class="tab-pane" id="tab2-2">
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Diagnosis</label>
<div class="col-sm-5 col-lg-5 controls">
<form:textarea path="diagnosis" name="Diagnosis"
class="form-control" rows="4"></form:textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Treatment</label>
<div class="col-sm-5 col-lg-5 controls">
<form:textarea path="treatment" name="Treatment"
class="form-control" rows="4"></form:textarea>
</div>
</div>
<div class="form-group">
<label for="firstname2"
class="col-sm-3 col-lg-2 control-label">Anesthesia</label>
<div class="col-sm-5 col-lg-3 controls">
<form:input path="anesthesia" name="Anesthesia" type="text"
class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Clinical
Presentation</label>
<div class="col-sm-5 col-lg-5 controls">
<form:textarea path="clinical_presentation"
name="clinicalpresentation" class="form-control" rows="4"></form:textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Investigation</label>
<div class="col-sm-5 col-lg-5 controls">
<form:textarea path="investigation" name="Investigation"
class="form-control" rows="4"></form:textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Given
Treatment</label>
<div class="col-sm-5 col-lg-5 controls">
<form:textarea path="given_treatment" name="giventreatment"
class="form-control" rows="4"></form:textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Treatment
Advice on Discharge</label>
<div class="col-sm-5 col-lg-5 controls">
<form:textarea path="advice" name="Advice"
class="form-control" rows="4"></form:textarea>
</div>
</div>
</div>
<div class="tab-pane" id="tab2-3">
<div class="form-group">
<label for="country2" class="col-sm-3 col-lg-2 control-label">Country</label>
<div class="col-sm-5 col-lg-3 controls">
<input type="text" name="country2" id="country2"
class="form-control">
</div>
</div>
<div class="form-group">
<label for="city2" class="col-sm-3 col-lg-2 control-label">City</label>
<div class="col-sm-5 col-lg-3 controls">
<input type="text" name="city2" id="city2"
class="form-control">
</div>
</div>
<%-- <div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Gender</label>
<div class="col-sm-5 col-lg-3 controls">
<label class="radio-inline">
<form:radiobutton path="sex" name="gender" value="Male" /> Male
</label> <label class="radio-inline">
<form:radiobutton path="sex" name="gender" value="Female" /> Female
</label>
</div>
</div> --%>
<div class="form-group">
<label for="textarea2" class="col-sm-3 col-lg-2 control-label">More
info</label>
<div class="col-sm-5 col-lg-3 controls">
<textarea name="textarea2" id="textarea2" rows="5"
class="form-control"></textarea>
</div>
</div>
</div>
<div class="tab-pane" id="tab2-4">
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">First
Name</label>
<div class="col-sm-5 col-lg-3 controls">
<span class="text">Penny</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">last
Name</label>
<div class="col-sm-5 col-lg-3 controls">
<span class="text">Rodrigez</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Gender</label>
<div class="col-sm-5 col-lg-3 controls">
<span class="text">Female</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Username</label>
<div class="col-sm-5 col-lg-3 controls">
<span class="text">PennyRod</span>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-lg-2 control-label">Email</label>
<div class="col-sm-5 col-lg-3 controls">
<span class="text">pennyrod#domain.com</span>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-3 col-lg-10 col-lg-offset-2">
Back Continue
<button type="submit" class="btn btn-success button-submit">Submit</button>
</div>
</div>
</div>
</form:form>
</div>
</div>
</div>
</div>
</div>
<!-- END Main Content -->
<jsp:include page="footer.jsp"></jsp:include>

How to use jsp tag for select?

I have a view model and one of the fields has to be mapped to a dropdown. I manage to populate the dropdown but i don't know how to specify the selected value.
edituser.jsp
<%# include file="/WEB-INF/template/taglibs.jsp"%>
<div class="container">
<%# include file="menu.jsp"%>
<form:form commandName="editForm" method="post" class="form-horizontal form-width">
<fieldset>
<legend>Edit user</legend>
<div class="form-group">
<input class="hidden" type="text" value="${id}" hidden="true" name="id"/>
<label for="firstName" class="col-lg-2 control-label">First
name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="firstName"
placeholder="First name" name="firstName" value="${fname}">
</div>
</div>
<div class="form-group">
<label for="lastName" class="col-lg-2 control-label">Last
name</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="lastName"
placeholder="Last name" name="lastName" value="${lname}">
</div>
</div>
<div class="form-group">
<label for="select" class="col-lg-2 control-label">Role</label>
<div class="col-lg-10">
<select name="role" class="form-control">
<option value="NONE">Select role</option>
<c:forEach items="${roles}" var="role">
<option value="${role}">${role}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label for="username" class="col-lg-2 control-label">Username</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="username"
placeholder="Username" name="username" value="${username}">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-lg-2 control-label">Password</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="inputPassword"
placeholder="Password" name="password" value="${guid}">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-lg-2 control-label">Confirm password</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="inputPassword"
placeholder="Password" name="confirmedpassword" value="${guid}">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<a class="btn btn-warning" href="http://localhost:8080/Catering/index/users">Cancel</a>
<button type="submit" class="btn btn-warning">Submit</button>
</div>
</div>
</fieldset>
</form:form>
</div>
UsersController
#Controller
public class UsersController {
#RequestMapping(value = "/editUser", method = RequestMethod.GET)
public String getEditUserPage(#RequestParam int id, Model model, Authentication authentication) {
logger.debug("Received request to show edit user page(GET)");
model.addAttribute("username", "You are logged in as " + authentication.getPrincipal());
UserModel user = UserDataAccess.getUserById(id);
model.addAttribute("id", user.getId());
model.addAttribute("fname", user.getFirstName());
model.addAttribute("lname", user.getLastName());
model.addAttribute("role", user.getRole());
model.addAttribute("username", user.getUsername());
model.addAttribute("guid", guid);
model.addAttribute("editForm", new UserViewModel());
return "edituser";
}
}
UserViewModel
public class UserViewModel {
private int id;
private String firstName;
private String lastName;
private String role;
private String username;
private String password;
private String confirmedpassword;
//getters and setters
}
You can do something like (not tested)
<select name="role" class="form-control">
<option value="NONE">Select role</option>
<c:forEach items="${roles}" var="curRole">
<c:choose>
<c:when test="${curRole eq role}">
<option selected="selected">${role}</option>
</c:when>
<c:otherwise>
<option>${role}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
You also must add a Collection in your controller, representing the list of all roles. I cannot see it from your code.
I suggest you to take a look at spring-form.tld facilities, in your particular case: select tags: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/spring-form.tld.html#spring-form.tld.select
Here you can find a simple example on how using spring:form tag: http://www.mkyong.com/spring-mvc/spring-mvc-dropdown-box-example/

Resources