无法从JSP

时间:2018-06-06 10:54:32

标签: java spring jsp model-view-controller requestdispatcher

在我的JSP表单中,环境可以识别控制器的存在(它为我提供了自动完成的路径),但是我无法访问我的控制器。

可能是因为配置设置不好,但这里的所有解决方案都是使用xml配置的。

这是我的表格:

<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>

控制器:

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;
    }
}

和我的配置类:

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;
    }
}

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();
    }
}

,错误是404,因为它无法识别控制器。

2 个答案:

答案 0 :(得分:0)

引起我注意的第一件事是你的RequestMapping应该使用value&#34; / registerUser&#34;并返回一些页面,而不只是null。所以我在评论中看到,那对你没有用。
您的配置似乎没问题,但我注意到您正在使用getServletConfigClasses()配置文件。在我的项目中,我不得不使用getRootConfigClasses(),因为如果没有root配置,我会遇到一些问题。所以,也许这也是你的问题:

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;
    }
}

答案 1 :(得分:-1)

代替action =“/ user / registerUser”
尝试 action =“$ {pageContext.request.contextPath} / user / registerUser”
让我知道你的网址扩展名,例如.jsp或.html或.do
例如: action =“$ {pageContext.request.contextPath} /user/registerUser.do”