首页 > 解决方案 > java spring mvc web项目找不到路由-请求的资源不可用

问题描述

这个初级 Spring MVC 项目是使用基于 Java 的配置进行配置的,项目结构如下所示:

在此处输入图像描述

前端控制器如下:

public class SiteFrontController extends AbstractAnnotationConfigDispatcherServletInitializer
{
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {MvcConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
}

'MvcConfig.java' 如下:

package com.tz.web;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan("com.tz")
public class MvcConfig implements WebMvcConfigurer
{

}

单控制器如下:

package com.tz.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AddController 
{
    @RequestMapping("/add")
    public ModelAndView add(@RequestParam("o1") int num1, @RequestParam("o2") int num2) 
    {
        ModelAndView mv = new ModelAndView();
        int k = num1 + num2;
        mv.addObject("result", k);
        mv.setViewName("result.jsp");
        return mv;
    }
}

要调用此控制器/路由,格式如下:

<form action="add">
  <input type="text" name="o1"><br>
  <input type="text" name="o2"><br>
  <input type="submit">
</form>

运行是在 Tomcat v10.0 上完成的,它运行,但它一直找不到控制器路由,它一直说:

Message The requested resource [/jproj/add] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

标签: javaspring-bootspring-mvc

解决方案


推荐阅读