DEV Community

Edit Item in Intellij IDEA

arotto8719 on May 21, 2020

@RequestMapping(value="edit/{postId}", method = RequestMethod.GET) public String displayEditForm(Model model, @PathVariable int postId, Error e...
Collapse
 
raviyasas profile image
Ravi Yasas

You need to autowire the repository like below

@Autowired
    private ArtifactRepository artifactRepository;

    @RequestMapping(value="edit", method= RequestMethod.POST)
    public String processEditForm(@RequestParam(value="post_id") int post_id, @RequestParam(value="name") String name, @RequestParam(value="location") String location, @RequestParam(value="description") String description, Model model) {

        ThePost thePost = artifactRepository.findById(post_id).get();
        thePost.setName(name);
        thePost.setLocation(location);
        thePost.setDescription(description);
        artifactRepository.save(thePost);
        return "index";
    }

Collapse
 
declantreanor profile image
declanTreanor

in which case the OP would have gotten a Nullpointer, which wasn't shared. A bit more detail would be helpful but, certainly, not autowiring that dependency would cause problems.

Collapse
 
arotto8719 profile image
arotto8719

I have no idea what a null pointer is

Collapse
 
arotto8719 profile image
arotto8719
Collapse
 
arotto8719 profile image
arotto8719

Trying But I’m having trouble uploading photos. Any ideas?

Collapse
 
arotto8719 profile image
arotto8719

github.com/arotto8719/app

The whole code is on here.

Collapse
 
raviyasas profile image
Ravi Yasas

When we are creating a Spring web application, the business logic should be in a separate layer as a Service. So we have Controller, Service, Repositories, and Models. Please check this github.com/raviyasas/spring-boot-hibernate-demo.git

Thread Thread
 
arotto8719 profile image
arotto8719 • Edited

I have all that on there but I need to know what to put in the exact lines so I can save and edited item.

Something besides the Post or whatever.

I have artifact repository.

Am I supposed to copy and paste what you have or do I have to make mine similar to yours?

Thread Thread
 
raviyasas profile image
Ravi Yasas

It is better if you can write your own code like mine or you can use my one. It is the best practice to write code in mvc pattern. That is why I mentioned it.

Collapse
 
arotto8719 profile image
arotto8719

I already have that on top of home controller

Collapse
 
arotto8719 profile image
arotto8719

@RequestMapping(value="edit/{postId}", method = RequestMethod.GET)
public String displayEditForm(Model model, @PathVariable int postId, Error error){
Artifact thePost = artifactRepository.findById(postId).get();
artifactRepository.save(thePost);
model.addAttribute("thePost", thePost);
return "edit";
}

@RequestMapping(value="edit/{post_id}", method=RequestMethod.POST)
public String processEditForm(@RequestParam(value="edit/{post_id}") @PathVariable int post_id, @RequestParam(value="name") String name, @RequestParam(value="location") String location, @RequestParam(value="description") String description, Model model) {
    Artifact thePost = artifactRepository.findById(post_id).get();
    thePost.setName(name);
    thePost.setLocation(location);
    thePost.setDescription(description);
    artifactRepository.save(thePost);
    return "index";
}

This is the problem area. I need exact words and descriptions on what it's supposed to look like.

Collapse
 
arotto8719 profile image
arotto8719

package org.launchcode.app.controllers;

import org.launchcode.app.data.Artifact;
import org.launchcode.app.data.ArtifactRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import javax.validation.Valid;
import java.util.HashMap;

@Controller
public class HomeController {

@Autowired
private ArtifactRepository artifactRepository;

@RequestMapping(value = "/")
public String index(Model model) {

// model.addAttribute("database", "Database");
// model.addAttribute("images", "Images");
model.addAttribute("add", "Add");
model.addAttribute("search", "Search");
model.addAttribute("edit", "Edit");
model.addAttribute("delete", "Delete");
// model.addAttribute("thePost", thePost);
model.addAttribute("artifacts", artifactRepository.findAll());
HashMap actionChoices = new HashMap<>();
return "index";
}

@GetMapping("add")
public String displayAddArtifactForm(Model model) {
    model.addAttribute(new Artifact());
    model.addAttribute("artifact", new Artifact());
    return "add";
}

@PostMapping("add")
@ResponseBody
public String processAddArtifactForm(@ModelAttribute @Valid Artifact newArtifact,
                                     Errors errors) {
    if (errors.hasErrors()) {
        return "add";
    } else {
        artifactRepository.save(newArtifact);
        return "index";
    }
}

@RequestMapping(value="edit/{postId}", method = RequestMethod.GET)
public String displayEditForm(Model model, @PathVariable int postId, Error error){
    Artifact thePost = artifactRepository.findById(postId).get();
    artifactRepository.save(thePost);
    model.addAttribute("thePost", thePost);
    return "edit";
}

@RequestMapping(value="edit/{post_id}", method=RequestMethod.POST)
public String processEditForm(@RequestParam(value="edit/{post_id}") @PathVariable int post_id, @RequestParam(value="name") String name, @RequestParam(value="location") String location, @RequestParam(value="description") String description, Model model) {
    Artifact thePost = artifactRepository.findById(post_id).get();
    thePost.setName(name);
    thePost.setLocation(location);
    thePost.setDescription(description);
    artifactRepository.save(thePost);
    return "index";
}
@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
public String delete(@PathVariable("id") int id) {
        artifactRepository.deleteById(id);
    return "index";
}

@GetMapping("view/{artifactId}")
public String displayViewArtifactId(Model model, @PathVariable int Id) {
    return "index";
}

// @GetMapping("view/{Images}")
// public String displayViewImagesId(Model model, @PathVariable int Id) {
// return "images";
// }
}

Ok moving on
This is my entire home controller.

Collapse
 
arotto8719 profile image
arotto8719

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri May 22 17:14:24 CDT 2020
There was an unexpected error (type=Bad Request, status=400).
Required int parameter 'edit/{post_id}' is not present
org.springframework.web.bind.MissingServletRequestParameterException: Required int parameter 'edit/{post_id}' is not present
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:204)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:114)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:830)

Here's the error page.
What's the answer?

Collapse
 
arotto8719 profile image
arotto8719

Here’s the photo

thePost thePost didn’t work either

Collapse
 
arotto8719 profile image
arotto8719

how do I add photos of this problem on my phone to this site?

This is for a class I'm in.

Collapse
 
arotto8719 profile image
arotto8719

What it looks like

Collapse
 
arotto8719 profile image
arotto8719

Post not supported