gpt4 book ai didi

java - 如何使用基于Java的配置解决Spring MVC中的 "noHandlerFound"?

转载 作者:行者123 更新时间:2023-12-02 05:00:00 25 4
gpt4 key购买 nike

我正在使用基于 Java 的配置开发 Spring MVC 应用程序。我遇到此错误 org.springframework.web.servlet.DispatcherServlet noHandlerFound警告:在 DispatcherServlet 中未找到带有 URI [/solution/clients/css/form.css] 的 HTTP 请求的映射名为“调度员”

AppConfig.java

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = "solution")
@PropertySource("classpath:persistence-mysql.properties")
public class AppConfig {
@Autowired
private Environment env;

private Logger logger = Logger.getLogger(getClass().getName());

@Bean
public ViewResolver viewResolver() {

InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");

return viewResolver;
}
}

SolutionDispatcherServletInitializer.java

public class SolutionDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

Controller 类:

@Controller
@RequestMapping("/clients")
public class ClientsController {

// Injection of Service Class
@Autowired
private ClientService clientService;

@GetMapping("/list")
public String listClients(Model theModel) {

// Getting Clients From SERVICE Class
List<Clients> theClients = clientService.getClients();

// Adding Clients to Model(Entity Class)
theModel.addAttribute("clientsModel", theClients);

return "list-clients";
}
}

查看:

<h3>ALL CLIENTS</h3>
<div class="addnew-form">

<table>
<tr>
<th class="trtd">First Name</th>
<th class="trtd">Last Name</th>
<th class="trtd">Email</th>
<th class="trtd">Mobile Number</th>
<th class="trtd">City</th>
<th class="trtd">Country</th>
<th class="trtd">Domain</th>
<th class="trtd">View</th>
<th class="trtd">Action</th>
</tr>

<!-- LOOP -->

<c:forEach var="tempClients" items="${clientsModel}">
<tr>
<td class="trtd">${tempClients.firstName}</td>
<td class="trtd">${tempClients.lastName}</td>
<td class="trtd">${tempClients.email}</td>
<td class="trtd">${tempClients.mobileNumber}</td>
<td class="trtd">${tempClients.city}</td>
<td class="trtd">${tempClients.country}</td>
<td class="trtd">${tempClients.domain}</td>
</tr>
</c:forEach>
</table>

错误消息:

May 31, 2019 12:40:22 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/solution/clients/css/form.css] in DispatcherServlet with name 'dispatcher'
WARNING: No mapping found for HTTP request with URI [/solution/$%7BpageContext.request.contextPath%7D/clients/list] in DispatcherServlet with name 'dispatcher'

Web 浏览器上的输出看起来像这样。我的数据库与应用程序有连接。但仍然无法从MySql获取数据。输出显示一些 JSP 代码。 ${tempClients.firstName}

图片:Click here for Image

提前致谢

最佳答案

我认为你需要将@Controller注释更改为@RestController。

所以你的 Controller 会像

@RestController
@RequestMapping("/clients")
public class ClientsController {

// Injection of Service Class
@Autowired
private ClientService clientService;

@GetMapping("/list")
public String listClients(Model theModel) {

// Getting Clients From SERVICE Class
List<Clients> theClients = clientService.getClients();

// Adding Clients to Model(Entity Class)
theModel.addAttribute("clientsModel", theClients);

return "list-clients";
}
}

关于java - 如何使用基于Java的配置解决Spring MVC中的 "noHandlerFound"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56391376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com