如何使用基于Java的配置解决Spring MVC中的“ noHandlerFound”?

时间:2019-05-31 08:27:04

标签: java spring hibernate

我正在开发基于Java的Spring MVC应用程序。我遇到此错误org.springframework.web.servlet.DispatcherServlet noHandlerFoundWARNING: No mapping found for HTTP request with URI [/solution/clients/css/form.css] in DispatcherServlet with name 'dispatcher'

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

预先感谢

1 个答案:

答案 0 :(得分:0)

我认为您需要将@Controller批注更改为@RestController。

所以您的控制器会像

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