mercredi 6 mars 2019

Whitelabel Error Page (This application has no explicit mapping for /error, so you are seeing this as a fallback)

What is the problem? Can someone plz help me?

@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
@ComponentScan({"com.shatel.controller", "com.shatel.upload"})
public class WebServiceApplication {

public static void main(String[] args) throws Exception {
    SpringApplication.run(WebServiceApplication.class, args);

}

@Bean
CommandLineRunner init(StorageService storageService) {
    return (args) -> {
        storageService.deleteAll();
        storageService.init();
    };
}
}

SpringSpringMVCController:

@Controller
public class SpringMVCController 
{

@RequestMapping(value="/processForm", params="ReleaseMsisdn", method=RequestMethod.POST)
public void releaseMsisdn() throws IOException
{

    Sending.msisdn();

}
@RequestMapping(value="/processForm",params="action2",method=RequestMethod.POST)
public void action2()
{
    System.out.println("Action2 block called");
}
}

FileSystemStorageService:

@Service
public class FileSystemStorageService implements StorageService {

private final Path rootLocation;

@Autowired
public FileSystemStorageService(StorageProperties properties) {
    this.rootLocation = Paths.get(properties.getLocation());
}

@Override
public void store(MultipartFile file) {
    String filename = StringUtils.cleanPath(file.getOriginalFilename());
    try {
        if (file.isEmpty()) {
            throw new StorageException("Failed to store empty file " + filename);
        }
        if (filename.contains("..")) {
            // This is a security check
            throw new StorageException(
                    "Cannot store file with relative path outside current directory "
                            + filename);
        }
        try (InputStream inputStream = file.getInputStream()) {
            Files.copy(inputStream, this.rootLocation.resolve(filename),
                StandardCopyOption.REPLACE_EXISTING);
        }
    }
    catch (IOException e) {
        throw new StorageException("Failed to store file " + filename, e);
    }
}

@Override
public Stream<Path> loadAll() {
    try {
        return Files.walk(this.rootLocation, 1)
            .filter(path -> !path.equals(this.rootLocation))
            .map(this.rootLocation::relativize);
    }
    catch (IOException e) {
        throw new StorageException("Failed to read stored files", e);
    }

}

@Override
public Path load(String filename) {
    return rootLocation.resolve(filename);
}

@Override
public Resource loadAsResource(String filename) {
    try {
        Path file = load(filename);
        Resource resource = new UrlResource(file.toUri());
        if (resource.exists() || resource.isReadable()) {
            return resource;
        }
        else {
            throw new StorageFileNotFoundException(
                    "Could not read file: " + filename);

        }
    }
    catch (MalformedURLException e) {
        throw new StorageFileNotFoundException("Could not read file: " + filename, e);
    }
}

@Override
public void deleteAll() {
    FileSystemUtils.deleteRecursively(rootLocation.toFile());
}

@Override
public void init() {
    try {
        Files.createDirectories(rootLocation);
    }
    catch (IOException e) {
        throw new StorageException("Could not initialize storage", e);
    }
}
}

FileUploadController:

 @Controller
 public class FileUploadController {

private final StorageService storageService;

@Autowired
public FileUploadController(StorageService storageService) {
    this.storageService = storageService;
}

@GetMapping("/")
public String listUploadedFiles(Model model) throws IOException {

    model.addAttribute("files", storageService.loadAll().map(
            path -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,
                    "serveFile", path.getFileName().toString()).build().toString())
            .collect(Collectors.toList()));

    return "uploadForm";
}

@GetMapping("/files/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String filename) {

    Resource file = storageService.loadAsResource(filename);
    return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
            "attachment; filename=\"" + file.getFilename() + "\"").body(file);
}

@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
        RedirectAttributes redirectAttributes) {

    storageService.store(file);
    redirectAttributes.addFlashAttribute("message",
            "You successfully uploaded " + file.getOriginalFilename() + "!");

    return "redirect:/";
}

@ExceptionHandler(StorageFileNotFoundException.class)
public ResponseEntity<?> handleStorageFileNotFound(StorageFileNotFoundException exc) {
    return ResponseEntity.notFound().build();
}

}

StorageException:

public class StorageException extends RuntimeException {

public StorageException(String message) {
    super(message);
}

public StorageException(String message, Throwable cause) {
    super(message, cause);
}
}

StorageFileNotFoundException:

public class StorageFileNotFoundException extends StorageException {

public StorageFileNotFoundException(String message) {
    super(message);
}

public StorageFileNotFoundException(String message, Throwable cause) {
    super(message, cause);
}
}

StorageProperties:

@ConfigurationProperties("upload")
public class StorageProperties {

private String location = "upload-dir";

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

}

StorageService:

public interface StorageService {

void init();

void store(MultipartFile file);

Stream<Path> loadAll();

Path load(String filename);

Resource loadAsResource(String filename);

void deleteAll();

}

and at last the uploadForm.html:

   <form:form action="processForm" method="post">
        <input type = "submit" name = "ReleaseMsisdn" value="Release Msisdn"/>
         <input type = "submit" name = "action2" value="Action2"/> 
</form:form>

<div th:if="${message}">
    <h2 th:text="${message}"/>
</div>

<div>
    <form method="POST" enctype="multipart/form-data" action="/">
        <table>
            <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
            <tr><td></td><td><input type="submit" value="Upload" /></td></tr>
        </table>
    </form>
</div>

<div>
    <ul>
        <li th:each="file : ${files}">
            <a th:href="${file}" th:text="${file}" />
        </li>
    </ul>
</div>

when I run this, it gives me this error on http://localhost:8080/

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Not Found, status=404). No message available

what should I do? and I use Spring suit tool and in the ulpoadForm, it says: Unknown tag (form:form)

tnx :)




Aucun commentaire:

Enregistrer un commentaire