Why update method isn't calling? - spring

I have a problem. When I click to update image on form, update method isn't calling. I see that in my output logs window. Only may update image in first row in my table.
When I updating first row:
Hibernate: update shoe_goods set description=?, image=?, in_stock=?,
name=?, price=?, size=?, web_store_id=? where id=?
When other rows - nothing.
I save my images in db as byte.
This is my method to display images:
Controller:
#RequestMapping(value = "/imageController/{id}")
#ResponseBody
public byte[] getImages(#PathVariable int id) {
return shoeGoodsService.getById(id).getImage();
}
JSP:
<img src="/imageController/${goods.id}" alt="Image" />
Code in DAO class:
#Override
public void update(ShoeGoods shoeGoods) {
sessionFactory.getCurrentSession().merge(shoeGoods);
sessionFactory.getCurrentSession().getTransaction().commit();
}
Code in Service class:
#Override
public void update(ShoeGoods shoeGoods) {
shoeGoodsDAO.update(shoeGoods);
}
Code in controller, when update method is call only for first row in table:
#RequestMapping(value="/upload_shoe_image", method = RequestMethod.POST)
public #ResponseBody ModelAndView handleFileUpload1(#RequestParam("file") MultipartFile file){
...
try {
ShoeGoods shoeGoods = shoeGoodsService.getById(goodsId);
shoeGoods.setImage(file.getBytes());
//not call always
shoeGoodsService.update(shoeGoods);
} catch (Exception e) {
e.printStackTrace();
}
...
}
In JSP:
<form id="send-image-form" method="POST" enctype="multipart/form-data"
action="/upload_shoe_image">
Edit: <input type="file" name="file">
<br />
Save: <input type="submit" id="save-image" value="Upload">
</form>

Related

Trying to create a dropdown in my jsp page

I am trying to create a dropdown which will have dynamic values in my jsp page but getting an exception, i am trying to use spring form tags here.
WebController.java
#RequestMapping(value="/addAchivement",method=RequestMethod.GET)
public String addAchievements(){
Object object=null;
try {
object = genericAppProcessor.checkLogin(username, password,null,null);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SchoolLoginDetails sld=new SchoolLoginDetails();
sld=(SchoolLoginDetails)object;
List<GroupDetails> list=new ArrayList<GroupDetails>();
list=(List<GroupDetails>) sld.getGroupDetails();
Set<Object> addedClass = new HashSet<Object>();
Set<Object> addedSection = new HashSet<Object>();
Map referenceData = new HashMap();
Map<Object,Object> classs = new LinkedHashMap<Object,Object>();
for(int i=0;i<list.size();i++){
Object obj=list.get(i).getClazz();
Object objj=list.get(i).getSection();
addedClass.add(obj);
addedSection.add(objj);
List<Object> convertTolist=new ArrayList<Object>(addedClass);
classs.put(convertTolist.get(0),convertTolist.get(0));
addedClass.clear();
}
referenceData.put("classList",classs);
return "addAchivement";
}
addAchivement.jsp
<form:form method="POST" role="form" action="/GenericApp/addWebAchievement" enctype="multipart/form-data">
<form:select path="classs">
<form:options items="${classList}" />
</form:select>
Exception :-
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/addAchivement.jsp at line 72
<div class="input-field col s12 m4 l3" >
<!--Line 72 --> <form:select path="classs" class="text-black custom-select">
<form:options items="${classList}" />
</form:select>
you need to return model and view instead of just returning a view Use Below Code :
// Java Code
#RequestMapping(value="/addAchivement",method=RequestMethod.GET)
public ModelAndView addAchievements(){
Object object=null;
try {
object = genericAppProcessor.checkLogin(username, password,null,null);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SchoolLoginDetails sld=new SchoolLoginDetails();
sld=(SchoolLoginDetails)object;
List<GroupDetails> list=new ArrayList<GroupDetails>();
list=(List<GroupDetails>) sld.getGroupDetails();
Set<Object> addedClass = new HashSet<Object>();
Set<Object> addedSection = new HashSet<Object>();
Map referenceData = new HashMap();
Map<Object,Object> classs = new LinkedHashMap<Object,Object>();
for(int i=0;i<list.size();i++){
Object obj=list.get(i).getClazz();
Object objj=list.get(i).getSection();
addedClass.add(obj);
addedSection.add(objj);
List<Object> convertTolist=new ArrayList<Object>(addedClass);
classs.put(convertTolist.get(0),convertTolist.get(0));
addedClass.clear();
}
referenceData.put("classList",classs);
ModelAndView mav = new ModelAndView("addAchivement", referenceData);
return mav;
}
You are assigning a path to classs in your jsp
<!--Line 72 --> <form:select path="classs" class="text-black custom-select">
but you are not sending anything from model on which this class variable can map so you need to use modelAttribute in your spring form so that this class variable can map .
for eg .
: you need to make a class object which has a class as member :
below is java code change :
referenceData.put("classList",classs);
referenceData.put("classObject",class); // Here class is a object that has class attribute by which that value in jsp will bind .
ModelAndView mav = new ModelAndView("addAchivement", referenceData);
Here is Jsp Code change :
<form:form method="POST" role="form" action="/GenericApp/addWebAchievement" enctype="multipart/form-data" modelAttribute="classObject">
<form:select path="classs">
<form:options items="${classList}" />
</form:select>
Sample class object :
public Class{
String classs ;
// getter setter for classs member variable .
}

unable to render index or mobile page with spring mobile

I was able to get my project to run just fine until I brought in spring-mobile. Now only Strings will return in the browser that the spring-mobile method is returning:
#Controller
public class DeviceDetection {
private Logger logger = LoggerFactory.getLogger(DeviceDetection.class);
#RequestMapping(value="/index")
public String detectDevice(Device device) {
if (device.isNormal()) {
System.out.println("Inside isNormal()");
return "index";
} else if (device.isMobile()) {
System.out.println("Inside isMobile()");
return "mobilePage";
} else if (device.isTablet()) {
return "mobilePage";
}
return "index";
}
}
So I decided I needed internalResourceViewResolver but that just gives me the following error:
Error creating bean with name 'viewResolver' defined in class path resource
[org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoCon
figurationAdapter.class]: Initialization of bean failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'org.springframework.boot.autoconfigure.mobile
and
DeviceDelegatingViewResolverAutoConfiguration$DeviceDelegatingViewResolverConfigur
ation$InternalResourceViewResolverDelegateConfiguration.viewResolver; nested
exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type
[org.springframework.web.servlet.view.InternalResourceViewResolver] is
defined: expected single matching bean but found 2:
getViewResolver,defaultViewResolver
Resolver class
#Configuration
public class MvcConfiguration extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".html");
return resolver;
}
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Main Class
#SpringBootApplication
public class StidhamFinancialApplication extends SpringBootServletInitializer {
public static void main(String[] args) throws UnknownHostException {
SpringApplication app = new SpringApplication(StidhamFinancialApplication.class);
Environment env = app.run(args).getEnvironment();
System.out.println(String.format("Access URLs:\n----------------------------------------------------------\n\t" +
"Local: \t\thttp://127.0.0.1:%1s\n\t" +
"External: \thttp://%2s:%3s\n----------------------------------------------------------",
env.getProperty("server.port"),
InetAddress.getLocalHost().getHostAddress(),
env.getProperty("server.port")));
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(StidhamFinancialApplication.class);
}
}
project structure:
I am running it on Tomcat 8...
--------Update 1---------------
I deleted my configuration package and add #ResponeBody to my detectDevice method like so:
//If I remove #ResponseBody it complains of a Circular view path [index] with
#ResponseBody it just renders a String and not a view
#RequestMapping(value="/index")
public #ResponseBody String detectDevice(Device device) {
if (device.isNormal()) {
System.out.println("Inside isNormal()");
return "index";
} else if (device.isMobile()) {
System.out.println("Inside isMobile()");
return "mobilePage";
} else if (device.isTablet()) {
return "mobilePage";
}
return "index";
}
Only the String index or mobilePage will render in the browser. If I delete the #ResponseBody then the project gives an error saying the following:
2016-05-29 08:55:03.682 ERROR 8230 --- [nio-8080-exec-1]
o.s.boot.context.web.ErrorPageFilter : Forwarding to error page
from request [/index.html] due to exception [Circular view path
[index]: would dispatch back to the current handler URL [/index] again.
Check your ViewResolver setup! (Hint: This may be the result of an
unspecified view, due to default view name generation.)]
If I remove the /index then the method is ignored completely and spring-boot just maps my index file and works again but I need the mobile site to work.
-------------------UPDATE 2-------------------
Ok I added thymeleaf and my index page is rendering again. Even better the DeviceDetection is working but once my project looks for my mobilePage I get the following error:
Error resolving template "mobile/index", template might not exist or might not be accessible by any of the configured Template Resolvers
I had to add the follow ing #Configuration to get the project to work. The spring-boots defaultViewResolver was causing me to many issues. Here is the project structure now:
I updated the project on GIT too.
First I needed to use something like Thymeleaf and create a viewResolver. Spring-boots defaultViewResolver was causing me issues:
#Configuration
public class WebConfiguration extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
#Bean
public DispatcherServlet dispatcherServlet(){
DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
return dispatcherServlet;
}
#Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
return resolver;
}
}
Since I was using Thymeleaf I needed bindingResult and a few other things. This is also where I implemented springs mobile Device interface to detect the device.
#Controller
public class FormController {
#RequestMapping(value="/", method= RequestMethod.GET)
public String contactForm(#Valid #ModelAttribute("person") Contact contact, BindingResult bindingResult,
HttpServletRequest request, Model model, Device device) throws IOException {
if(bindingResult.hasErrors()){
System.out.println("There was a error "+bindingResult);
return "error";
}
model.addAttribute("contact", new Contact());
if(device.isNormal()){
System.out.println("I am normal");
return "index";
} else if(device.isMobile()){
System.out.println("I am a mobile");
return "mobilePage";
} else if (device.isTablet()){
System.out.println("I am a tablet");
return "mobilePage";
}
return "index";
}
Now I just needed to put the different views in the correct directories like so:
That was it and it is working perfectly.
Here is the form I created to use implement Thymeleaf:
<!-- Contact form -->
<form action="#" th:action="#{/contact}" th:object="${contact}" method="POST" class="contact-form clear-fix">
<div class="clear-fix">
<ul class="list-0 clear-fix">
<!-- Name -->
<li>
<div class="block field-box">
<label for="contact-form-name" class="text-color-black">Your Name</label>
<input type="text" th:field="*{name}" class="contact noMarr col-md-6 text-color-black" id="contact-form-name" />
</div>
</li>
<!-- /Name -->
<!-- E-mail address -->
<li>
<div class="block field-box">
<label for="contact-form-mail" class="text-color-black">Your E-mail</label>
<input type="text" th:field="*{email}" class="contact noMarr col-md-6 text-color-black" id="contact-form-mail" />
</div>
</li>
<!-- /E-mail address -->
<!-- Website URL -->
<li>
<div class="block field-box">
<label for="contact-form-subject" class="text-color-black">Subject</label>
<input type="text" th:field="*{subject}" class="contact noMarr col-md-6 text-color-black" id="contact-form-subject" />
</div>
</li>
<!-- /Website URL -->
<!-- Message -->
<li>
<div class="block field-box ">
<label for="contact-form-message" class="text-color-black">Your message</label>
<textarea name="comment" class="contact col-md-12 text-color-black" th:field="*{message}" id="contact-form-message"></textarea>
</div>
</li>
<!-- /Message -->
<!-- Submit button -->
<li>
<div class="block field-box field-box-button text-color-black">
<input type="submit" id="contact-form-submit" name="contact-form-submit" class="button text-color-black" value="Submit"/>
</div>
</li>
<!-- /Submit button -->
</ul>
</div>
</form>

wicket form event onsubmit not working at V6.X

I have a page like this.
HTML
<form wicket:id="form" onsubmit="alert('form submit')">
<input type="text" name="name">
<!-- Wicket1.5.X form.onsubmit working -->
<!-- Wicket6.X form.onsubmit can not work -->
<input wicket:id="ajaxFallbackButton" type="submit">
<!-- form.onsubmit works fine -->
<input wicket:id="submitBtn" type="submit">
</form>
Java
Form<Void> form = new Form<Void>("form");
form.setOutputMarkupId(true);
add(form);
AjaxFallbackButton ajaxFallbackButton = new AjaxFallbackButton("ajaxFallbackButton", new Model<String>("AjaxFallbackButton"), form) {
private static final long serialVersionUID = 1L;
#Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
System.out.println("ajaxFallbackButton onSubmit");
}
};
form.add(ajaxFallbackButton);
Button button = new Button("submitBtn", new Model<String>("Button"));
form.add(button);
Why the event of form onsubmit defined int the html can not work when I used Wicket V6.X?
Thanks for svenmeier, I fixed it by override the AjaxFallbackButton.updateAjaxAttributes method.
#Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
AjaxCallListener ajaxCallListener = new AjaxCallListener();
ajaxCallListener.onPrecondition("alert('form submit')");
attributes.getAjaxCallListeners().add(ajaxCallListener);
}
Wicket 6 uses JavaScript event handlers, so your inline JavaScript is not executed.
Read here for further explanation and solutions:
Wicket AjaxButton and javascript condition

Validation Spring confusion to render error message

I try to render error message but it's not appears:
I have validator:
#Component
class ValidatorFBOSSearch implements Validator {
#SuppressWarnings("rawtypes")
#Override
public boolean supports(Class clazz) {
return FormBackObjectSearch.class.equals(clazz);
}
#Override
public void validate(Object obj, Errors error) {
FormBackObjectSearch fbos = (FormBackObjectSearch)obj;
if ("".equals(fbos.particularDate)){
error.rejectValue("particularDate", "required.listOfDates", "This field is required");
}
}
}
it have to cheak does my field in backing object for form have value inputed or not. rejectValue has three param. First - looks for a value in Model Attribute in my backing object, Secong looks for error message in properties file(my properties file located in resourses/error forlder) Third param in methos says if it won't be able to find message in error properties file will render it as default, here is my sniped of code in my servlet-context.xml
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
And this my xml config for messageSourse to get errors for Validator in servlet-context.xml:
<!-- Resolves error messages for validator from /Education/src/main/webapp/resources/errors-->
<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basename" value="errors/errormessages"/>
</beans:bean>
Here peace of my controller code for dealing with requests:
#RequestMapping(value="/search", method=RequestMethod.GET)
public void search(Model model) {
FormBackObjectSearch fbos = new FormBackObjectSearch();
model.addAttribute("fbosAttribute", fbos);
}
#RequestMapping(value ="/result", method=RequestMethod.POST)
public String extract(#RequestParam String nameOfInstitution,
#RequestParam String particularDate,
#RequestParam String typeOfInstitution,
#ModelAttribute("fbosAttribute") FormBackObjectSearch fbos,
BindingResult result, RedirectAttributes redirectAttributes,
Model model) throws Exception {
ValidatorFBOSSearch validatorFBOS = new ValidatorFBOSSearch();
validatorFBOS.validate(fbos, result);
if(result.hasErrors()) {
redirectAttributes.addFlashAttribute("fbosAttribute", fbos);
return "redirect:/search";
} else {
if(particularDate !="" && nameOfInstitution !="" && typeOfInstitution =="") {
controllerSupportClass.findWithDateAndName(nameOfInstitution, particularDate, model);
} else if(particularDate !="" && nameOfInstitution =="" && typeOfInstitution !="") {
controllerSupportClass.findWithAddedDateAndType(typeOfInstitution, particularDate, model);
} else if(particularDate !="" && nameOfInstitution =="" && typeOfInstitution ==""){
controllerSupportClass.findWithAddedDate(particularDate, model);
} else if(particularDate !="" && nameOfInstitution !="" && typeOfInstitution !="") {
throw new Exception("Search by choose all parameters is not exceptable");
} else {
throw new Exception("You didn't put any search parameters");
}
}
return "search";
}
And here is peace of my jsp:
<form:form action="result" method="post" modelAttribute="fbosAttribute" >
<table>
<tr>
<th>Date for extracting:</th>
<td><form:select path="particularDate">
<form:option value=""> -Choose date-</form:option>
<form:options items="${listOfDates}"></form:options>
</form:select> <td><form:errors path="particularDate" cssClass="error"/></td>
</td>
</tr>
</table>
</form:form>
The problem is thet I can not see error messages show up. I tried to use Flash Attributes to have error appears after riderect, but nothing heppend. cuz I found here that when I use rederect it makes my model and errors deleted and start new one. But How can I have advantage to use flash attibutes to solve my problem. Thank you
To register the validator for an specific controller you need can use the `#InitBimder' annotation.
#InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new ValidatorFBOSSearch());
}
For mor details how to register the validator see my answer at:
Validation in Spring MVC
I think your check statement is wrong:
if ("".equals(fbos.particularDate)){
If a user enters nothing, then a String will not be empty ( "" ), it will be null. Therefore use:
if (fbos.particularDate == null || fbos.particularDate.isEmpty())

JSF - Example login-ajax - Why it send the form?

Im trying JSF framework. Im a beginner. So i start with a easy tutorial. This is the code :
<h:form id="form1" prependId="false">
<h:outputScript name="jsf.js" library="javax.faces" target="head" />
<h:inputText value="#{user.name}" id="name"/>
<h:inputSecret value="#{user.password}" id="password"/>
<h:outputText value="#{user.greeting}" id="out"/>
<br/>
<h:commandButton value="Login" type="button" onclick="jsf.ajax.request(this, event, {execute: 'name password', render: 'out'}); return false;" />
</h:form>
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name = "user")
#RequestScoped
public class UserBean {
private String name = "";
private String password;
public String getName() { return name; }
public void setName(String newValue) { name = newValue; }
public String getPassword() { return password; }
public void setPassword(String newValue) { password = newValue; }
public String getGreeting() {
if(name.length()==0) {
return "Inserisci nickname";
} else {
return "Benvenuto "+name;
}
}
}
In "less words" (when i click on button) :
1 - thanks to execute: 'name password, I call the setName and setPassword (JavaBean methods) with an asynch call;
2 - than, thanks to render: 'out' I call getGreeting
So, execute will be translate as SET and render as GET.
What I would like to know is if I have well understand the process.
Because I have a misunderstanding : if I scan the traffik from client to server, I see that (trought the ajax call) it sends from client to server the form1, name and password (and, as MAGIC, name and password have got the value inserted by me).
So how is this possible?
Thanks to all!
That isn't magic. That's normal. Those values are entered in webbrowser. JS/Ajax has access to those values. JS/Ajax will send those values along. How else should server side/JSF know about the newly entered values?

Resources