-1

In my JSP form, environment can recognize controller's existence (it offers me autocomplete to it's path), but I cannot reach my controller.

It might be because of configurations aren't set well, but all solutions here are configured using xml.

This is my form:

<h2>Register</h2>
<!-- Contact form -->
<form class="register-form" action="/user/registerUser" method="post">
    <div class="form-group">
        <input class="form-control" name="username" placeholder="Username">
    </div>
    <div class="form-group">
        <input class="form-control" name="name" placeholder="Name">
    </div>
    <div class="form-group">
        <input class="form-control" name="lastName" placeholder="Last name">
    </div>
    <div class="form-group">
        <input class="form-control" name="email" placeholder="Email">
    </div>
    <div class="form-group">
        <input class="form-control" name="phone" placeholder="Phone number">
    </div>
    <div class="form-group">
        <input class="form-control" name="password" placeholder="Password" type="password">
    </div>
    <div class="form-group">
        <input class="form-control" name="rePassword" placeholder="Re-password" type="password">
    </div>
    <input type="submit" value="Register" class="button-transparent submit-button">
</form>

controller:

package app.controller;

import app.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @RequestMapping(value = "registerUser", method = RequestMethod.POST)
    public User registerUser(Model m, HttpServletRequest request) {
        System.out.println("HERE");
        return null;
    }
}

and my configuration classes:

package app.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    protected String[] getServletMappings() {
        String[] init = {"/"};
        return init;
    }
}

and

package app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("app")
public class WebConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

and the error is 404 since it cannot recognize the controller.

nemanjagajic
  • 39
  • 12
  • before submitting the form what is the url in the browser? – Jonathan Laliberte Jun 06 '18 at 10:57
  • 5
    replace `"registerUser"` by `"/registerUser""` in controller – Afridi Jun 06 '18 at 10:58
  • the url is http://localhost:8080/index.jsp?register before submitting, and after it's http://localhost:8080/user/registerUser (but not found) @JonathanLaliberte – nemanjagajic Jun 06 '18 at 11:16
  • @Afridi it has the same behavior – nemanjagajic Jun 06 '18 at 11:17
  • Why are you returning `null` in `registerUser(...)`?? try to return name of the jsp page instead or add `@ResponseBody` to that method – Afridi Jun 06 '18 at 11:33
  • Are you sure you have recompiled after making the change @Afridi suggested? That is the only obvious thing I can see that would cause it not to resolve the URL to hit your method. Edit: although I have noticed there is no webapp name in the URL you mentioned above (localhost:8080/index.jsp?register). Why is that? – Jon Betts Jun 06 '18 at 11:38
  • I might be wrong, but I'm returning null because at this point I just want to reach the controller and see if it prints out the message (later I will implement behavior). Yes, I have recompiled. Also, not sure why, but in past it worked for me without project name because i just run the concrete page in IntelliJ, but I've tried now with localhost:8080/Reserve/index.jsp?register (it's my project's name) and it's the same thing. – nemanjagajic Jun 06 '18 at 12:00

2 Answers2

0

the first thing that get my attention is that your RequestMapping should be using the value "/registerUser" and return some page, not just null. So I saw in the comments that just that didn't work for you.
Your configurations seems ok, but i notice that you are using the getServletConfigClasses() to your configuration files. In my project I had to use getRootConfigClasses(), because I had some problems if its was not with root config. So, maybe that can be your problem too:

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[0];
    }

    protected String[] getServletMappings() {
        String[] init = {"/"};
        return init;
    }
}
Robson Farias
  • 151
  • 1
  • 3
  • 14
  • Yea, I though / wasn't necessary because it used to work for me when I didn't put it in method's annotations, but now it's there. I've also changed not to return null now (though I think it's not the problem because it's not even reaching the method aka servlet), and now I've tried with your modification in AppInitializer and it still doesn't work. I guess it must be something small and stupid about servlet dispatcher configurations, but I just can't see it. – nemanjagajic Jun 06 '18 at 12:06
-1

in place of action="/user/registerUser"
try action="${pageContext.request.contextPath}/user/registerUser"
Let me know You url extension i.e.., like .jsp or .html or .do
Ex: action="${pageContext.request.contextPath}/user/registerUser.do"

SITA RAM
  • 1
  • 1
  • I've already tried with ${pageContext.request.contextPath}, and it hasn't changed. The url has no extension since I'm trying to call a controller which should be accessed by path provided with RequestMapping – nemanjagajic Jun 06 '18 at 11:20
  • localhost:8080/index.jsp?register or localhost:8080/user/registerUser are you not having any project name in your url??? and can you please check once your console. and your are returning null. if you return empty or null or nothing by default controller / viewResolver can consider url as a page name. that means have to be one module/folder with the name user and in that module you have to write one jsp page write registerUser.jsp. once try this i hope this will work for you – SITA RAM Jun 06 '18 at 11:38