运行Spring MVC项目时出现404错误

pinkon5k  于 2023-08-02  发布在  Spring
关注(0)|答案(1)|浏览(88)

我得到404错误,而运行 Spring MVC项目。我已经将root请求Map到我的home.jsp,并且我正在使用spring java annotation configuration。我看了我的Map,它看起来很好。请帮我解决这个问题,我找不到哪里出错了。
Issue

@Controller
public class HomeController {

    @GetMapping("/")
    public String home()
    {
        System.out.println("jjjjjjjjjjjjjjjjjj");
        return "home";
    }
}

字符串
Dispatcher Servlet配置

public class DispServler extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[] {ConfigurationClass.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] {"/"};
    }

}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.veera")
public class ConfigurationClass {
    
    @Bean
    public ViewResolver viewResolver()
    {
        InternalResourceViewResolver vr = new InternalResourceViewResolver();
        vr.setPrefix("/");
        vr.setSuffix(".jsp");
        return vr;
    }

}

的数据
项目结构
Project structure
我已经在我的Map方法中添加了System.out.println()语句,但它并没有在控制台中打印我的that语句。

@Controller
public class HomeController {

    @GetMapping("/")
    public String home()
    {
        System.out.println("jjjjjjjjjjjjjjjjjj");
        return "home";
    }
}


console log

bihw5rsg

bihw5rsg1#

我猜,你是想在浏览器中访问不正确的端点,虽然你在这里指定了根Map,所以,尝试访问localhost:8080/或者如果你想更改home JSP以Map到/Practice端点,请将next代码更改为next:这是:

@Controller
public class HomeController {

    @GetMapping("/")
    public String home()
    {
        System.out.println("jjjjjjjjjjjjjjjjjj");
        return "home";
    }
}

字符串
对此:

@Controller
public class HomeController {

    @GetMapping("/Practice") // this line
    public String home()
    {
        System.out.println("jjjjjjjjjjjjjjjjjj");
        return "home";
    }
}


这是因为**@GetMappingannotation现在将您的JSP页面Map到localhost:8080/Practice**

相关问题