0

im doing a web and upon testing it i encountered this , Request Sent by the Client was syntactically incorrect. i dont know which is the one giving the error, but to say, i have a model class that is artist.java. My jsp pages are artist,jsp and artistview.jsp. and it is being controlled by the artistcontroller.java. I am getting this error when i edit a an artist from my artistview.jsp. it is then directed to the edit portion of artist.jsp. after editing the details filling all details again, and i press edit artist, i get the error. i have a hunch that it is coming from the uploadprofilepic method, but cant figure it out. hope you could help me on this. thanks in advance. here are my codes.

artist.java (my model class)

package com.artistlabprod.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name="ARTIST")
public class Artist {

@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;

@Column(name="ARTISTNAME") 
private String artistName;

@Column(name="SURNAME") 
private String surname;

@Column(name="TALENT") 
private String talent;

@Column(name="AGE")
private int age;

@Column(name="HEIGHT")
private String height;

@Column(name="WEIGHT")
private String weight;

@Column(name="HAIRCOLOR") 
private String hairColor;

@Column(name="EYECOLOR") 
private String eyeColor;

@Column(name="ETHNICITY") 
private String ethnicity;

@Column(name="EXPERIENCE") 
private String experience;

@Column(name="PHOTO")
private String photo;

@Column(name="TWITTER") 
private String twitter;

@Column(name="FACEBOOK") 
private String facebook;

@Column(name="INSTAGRAM") 
private String instagram;



public Artist() {
}


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getArtistName() {
    return artistName;
}


public void setArtistName(String artistName) {
    this.artistName = artistName;
}



public String getSurname() {
    return surname;
}


public void setSurname(String surname) {
    this.surname = surname;
}


public String getTalent() {
    return talent;
}


public void setTalent(String talent) {
    this.talent = talent;
}


public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public String getHeight() {
    return height;
}

public void setHeight(String height) {
    this.height = height;
}

public String getWeight() {
    return weight;
}

public void setWeight(String weight) {
    this.weight = weight;
}

public String getHairColor() {
    return hairColor;
}

public void setHairColor(String hairColor) {
    this.hairColor = hairColor;
}

public String getEyeColor() {
    return eyeColor;
}

public void setEyeColor(String eyeColor) {
    this.eyeColor = eyeColor;
}

public String getEthnicity() {
    return ethnicity;
}

public void setEthnicity(String ethnicity) {
    this.ethnicity = ethnicity;
}

public String getExperience() {
    return experience;
}

public void setExperience(String experience) {
    this.experience = experience;
}

public String getPhoto() {
    return photo;
}   

public void setPhoto(String photo) {
    this.photo = photo;
}


public String getTwitter() {
    return twitter;
}


public void setTwitter(String twitter) {
    this.twitter = twitter;
}


public String getFacebook() {
    return facebook;
}


public void setFacebook(String facebook) {
    this.facebook = facebook;
}


public String getInstagram() {
    return instagram;
}


public void setInstagram(String instagram) {
    this.instagram = instagram;
}
}

artistcontroller.java

package com.artistlabprod.controller;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.artistlabprod.model.Artist;
import com.artistlabprod.service.ArtistService;

@Controller
public class ArtistController  {

private ArtistService artistService;




public ArtistService getArtistService() {
    return artistService;
}  

@Autowired(required=true)
@Qualifier(value="artistService")
public void setArtistService(ArtistService ps){
    this.artistService = ps;
}

@RequestMapping("/")
public String home(Model model) {
    model.addAttribute("listArtists", this.artistService.listArtists());
    return "home";

}


 @RequestMapping(value = "/index", method = RequestMethod.GET)
 public ModelAndView getHome() {
     ModelAndView modelandview = new ModelAndView ("index");
     return modelandview;
 }



 @RequestMapping(value = "/aboutus", method = RequestMethod.GET)
 public ModelAndView getAboutus() {
     ModelAndView modelandview = new ModelAndView ("aboutus");
     return modelandview;
 }

 @RequestMapping(value = "/contactus", method = RequestMethod.GET)
 public ModelAndView getContactus() {
     ModelAndView modelandview = new ModelAndView ("contactus");
     return modelandview;
 }



@RequestMapping(value = "/artists", method = RequestMethod.GET)
public String listArtists(Model model) {
    model.addAttribute("artist", new Artist());
    model.addAttribute("listArtists", this.artistService.listArtists());
    return "artist";
}


@RequestMapping(value = "/artistview", method = RequestMethod.GET)
public String listofArtists(Model model) {
    model.addAttribute("listArtists", this.artistService.listArtists());
    return "artistview";
}


@RequestMapping(value= "/artist/add", method = RequestMethod.POST)
public String addArtist(){ 
    return "redirect:/artistview";
}


@RequestMapping("/remove/{id}")
public String removeArtist(@PathVariable("id") int id){
    this.artistService.removeArtist(id);
    return "redirect:/artists";
}

@RequestMapping("/edit/{id}")
public String editArtist(@PathVariable("id") int id, Model model) {
    model.addAttribute("artist", this.artistService.getArtistById(id));     
    model.addAttribute("listArtists", this.artistService.listArtists());
    return "artist";
}


@RequestMapping(value = "/uploadProfilePic", method = RequestMethod.POST)
public String uploadBackground(@ModelAttribute("artist") Artist p, @RequestParam("file") MultipartFile file, Model model) {
    String name="";
    if(p.getId() == 0){
        //add a new inventory item
        this.artistService.addArtist(p);
        p.setPhoto("photo-"+ p.getId() + ".jpg");
        this.artistService.updateArtist(p);
        name = p.getPhoto();
    }else{
        //update an existing inventory item
        this.artistService.updateArtist(p);
    } 


    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            String rootPath = "C:" + File.separator + "Users"
                    + File.separator + "User Folder"                        
                    + File.separator + "Documents" 
                    + File.separator + "appdevupitdc" 
                    + File.separator + "workspace" 
                    + File.separator + ".metadata"
                    + File.separator + ".plugins" 
                    + File.separator + "org.eclipse.wst.server.core" 
                    + File.separator + "tmp1" 
                    + File.separator + "webapps" 
                    + File.separator + "img"
                    + File.separator + "artistphotos";


            File dir = new File(rootPath);
            if (!dir.exists())
                dir.mkdirs();

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath()
                    + File.separator + name);
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();                 

            //return listofArtists(model);
            return addArtist();
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
            return "You failed to upload " + name
                + " because the file was empty.";
    }
}

}

artist.jsp

<div class="container">
 <div class="row">  
<c:url var="addAction" value="/artist/add"></c:url>
<form:form action="uploadProfilePic" commandName="artist" enctype="multipart/form-data">
    <table class="table table-hover table-responsive table-striped" >
        <c:if test="${!empty artist.artistName}">
            <tr>
                <td><form:label path="id">
                        <spring:message text="ID" />
                    </form:label></td>
                <td><form:input path="id" readonly="true" size="8"
                        disabled="true" /> <form:hidden path="id" /></td>
            </tr>
        </c:if>
        <tr>
            <td><form:label path="artistName">
                    <spring:message text="Name" />
                </form:label></td>
            <td><form:input size="35" path="artistName" /> </td>


        </tr>
        <tr>
            <td><form:label path="surname">
                    <spring:message text="Surname" />
                </form:label></td>
            <td><form:input size="35" path="surname" /></td>
        </tr>

        <tr>
            <td><form:label path="talent">
                    <spring:message text="Talent" />
                </form:label></td>
            <td><form:input size="35" path="talent" /></td>
        </tr>

        <tr>
            <td><form:label path="age">
                    <spring:message text="Age" />
                </form:label></td>
            <td><form:input path="age" /></td>
        </tr>
        <tr>
            <td><form:label path="height">
                    <spring:message text="Height" />
                </form:label></td>
            <td><form:input path="height" /></td>
        </tr>
        <tr>
            <td><form:label path="weight">
                    <spring:message text="Weight" />
                </form:label></td>
            <td><form:input path="weight" /></td>
        </tr>
        <tr>
            <td><form:label path="hairColor">
                    <spring:message text="Hair Color" />
                </form:label></td>
            <td><form:input  size="35" path="hairColor" /></td>
        </tr>
        <tr>
            <td><form:label path="eyeColor">
                    <spring:message text="Eye Color" />
                </form:label></td>
            <td><form:input  size="35" path="eyeColor" /></td>
        </tr>
        <tr>
            <td><form:label path="ethnicity">
                    <spring:message text="Ethnicity" />
                </form:label></td>
            <td><form:input  size="35" path="ethnicity" /></td>
        </tr>           


        <tr>
            <td><form:label path="photo">
                    <spring:message text="Photo" />
                </form:label></td>
            <td><form:input type="file" name="file" path="" /></td>
        </tr>                   


        <tr>
            <td><form:label path="experience">
                    <spring:message text="Experience" />
                </form:label></td>
            <td><form:textarea rows="10" cols="75" path="experience" /></td>
        </tr>

        <tr>
            <td><form:label path="twitter">
                    <spring:message text="Twitter Link" />
                </form:label></td>
            <td><form:input size="35" path="twitter" /></td>
        </tr>

        <tr>
            <td><form:label path="facebook">
                    <spring:message text="Facebook Link" />
                </form:label></td>
            <td><form:input size="35" path="facebook" /></td>
        </tr>

        <tr>
            <td><form:label path="instagram">
                    <spring:message text="Instagram Link" />
                </form:label></td>
            <td><form:input size="35" path="instagram" /></td>
        </tr>



        <tr>
            <td colspan="2"><c:if test="${!empty artist.artistName}">
                    <input type="submit" class="btn btn-success"value="<spring:message text="Edit Artist"/>" />
                </c:if> <c:if test="${empty artist.artistName}">
                    <input type="submit" class="btn btn-primary" value="<spring:message text="Add Artist"/>" />
                </c:if></td>
                <td></td>
        </tr>

        <tr>
        <td>
        <a class="btn btn-info" href="<c:url value='/artistview' />"><i class="fa fa-list-ul fa-lg"></i> View List</a>
        </td>
        <td></td>
        </tr>

        <!-- for bottom line of table -->
        <tr>
        <td>
        </td>
        <td></td>
        </tr>



    </table>
</form:form>


</div>

artistview.jsp

    <div class="container">
        <div class="row">
            <div class="col-lg-12 text-center">
                <h2 class="section-heading">Artist List</h2>
                <h3 class="section-subheading text-muted">Lorem ipsum dolor sit amet consectetur.</h3>
            </div>
        </div>



<c:if test="${!empty listArtists}">
    <table class="table table-hover table-responsive table-striped">
        <tr>
            <th>Profile Picture</th>                
            <th>First Name</th>
            <th>Last Name</th>
            <th>Talent</th>
            <th>Age</th>
            <th>Height</th>
            <th>Weight</th>
            <th>Hair Color</th>
            <th>Eye Color</th>
            <th>Ethnicity</th>
            <th>Experience</th>
            <th>Twitter Link</th>
            <th>Facebook Link</th>
            <th>Instagram Link</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
        <c:forEach items="${listArtists}" var="artist">
            <tr>
             <!--  <td><div style= "height: 200px; width: 200px"><img style="height: 100%; width:100%" src="photo?id=${artist.id}"/> </div>-->
             <td> <img class="img-responsive" style="height: 100%; width:100%" src="/img/artistphotos/${artist.photo}" alt="" /> </td>
                <td>${artist.artistName}</td>
                <td>${artist.surname}</td>
                <td>${artist.talent}</td>
                <td>${artist.age}</td>
                <td>${artist.height}</td>
                <td>${artist.weight}</td>
                <td>${artist.hairColor}</td>
                <td>${artist.eyeColor}</td>
                <td>${artist.ethnicity}</td>
                <td>${artist.experience}</td>
                <td>${artist.twitter}</td>
                <td>${artist.facebook}</td>
                <td>${artist.instagram}</td>
                <td><a class="btn btn-success" href="<c:url value='/edit/${artist.id}' />"><i class="fa fa-pencil fa-lg"></i> Edit</a></td>
                <td><a class="btn btn-danger" href="<c:url value='/remove/${artist.id}' />"><i class="fa fa-trash-o fa-lg"></i> Delete</a></td>


            </tr>
        </c:forEach>
    </table>
</c:if>

there is no error showing in the console of eclipse, but here is the error im receiving from the browser:

HTTP Status 400 -

type Status report

message

description The request sent by the client was syntactically incorrect.
Apache Tomcat/8.0.21

also the url on top displays this:

http://localhost:8080/artistweb/edit/uploadProfilePic
zansan
  • 35
  • 1
  • 13
  • Could you add the exception stack trace you are getting? – Rohit Jun 12 '15 at 11:52
  • sure, wait, i will just copy it. – zansan Jun 12 '15 at 11:54
  • @Rohit, i added the error message i receiving in the main question, edited it and added. there is no exception error displayed at the console, only in the browser. – zansan Jun 12 '15 at 12:11
  • Did you configure MultipartResolver in servlet-config? check my answer here http://stackoverflow.com/questions/30681528/file-uploading-not-working-in-spring-mvc-using-spring-form/30682133#30682133 – jgr Jun 12 '15 at 13:10
  • yes, i have the MultipartResolver in servlet-config already configured – zansan Jun 12 '15 at 13:26

2 Answers2

0

You are missing

(HttpServletResponse response, HttpServletRequest request)

in your method calls?

public String uploadBackground(@ModelAttribute("artist") Artist p, @RequestParam("file") MultipartFile file, Model model, HttpServletResponse response, HttpServletRequest request)
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
  • i added (HttpServletResponse response, HttpServletRequest request) but it still the same error, nothing happened. :( – zansan Jun 12 '15 at 13:07
  • Hmm...can u try to run http://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-file-upload-example/ and then compare your setup? – Sheetal Mohan Sharma Jun 12 '15 at 13:13
  • yup, i ran it, somehow same, but im not having trouble with uploading and saving new files. what im having problem is when an already existing artist is edited, and after retrieveing their info and editing, when i click edit artist i get the error. :( – zansan Jun 12 '15 at 13:29
0

add in dispatcher-servlet.xml

     <!-- setting maximum upload size -->
    <beans:property name="maxUploadSize" value="10000000000000" />

</beans:bean>   
  • 1
    Please also add an explanation what this setting does and how it contributes to solving the problem. – rabejens Jun 12 '18 at 14:36