java - 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.
解决方案
推荐阅读
- scala - 有没有办法运行测试,但如果失败则不将整个套件标记为失败?
- php - 更改添加到购物车按钮颜色并禁用 Woocommerce 中的缺货产品
- spring-boot - java 8日期在spring boot put请求中为null
- android - 当我单击第一行中的单选按钮时,为什么还会单击另一行中的单选按钮?
- php - PHP libcurl socks5 不工作
- c - 错误:'->' 的类型参数无效(有 'Queue {aka struct Queue}
- c# - Elasticsearch.Net 和 NEST、IGetResponse 来记录 POCO?
- google-cloud-platform - Google Cloud SQL 导出无中断
- c - fclose 会立即释放文件吗?
- date - 提示:日期,%Sysfunc(year(&myvar.));