Spring MVC 4.0.1请求映射不起作用

时间:2017-04-15 17:32:51

标签: java spring-mvc

我是Spring MVC的新手,试过各种各样的例子,但没有用 我使用NetBEan IDE 8.2 Spring 4.0.1 Java 1.8和 Apache Tomcat 8.0.27

这里是我的Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
</web-app>

My Dispath-servelet.xml

    <?xml version='1.0' encoding='UTF-8' ?>
    <!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <mvc:annotation-driven />
    <context:component-scan base-package="com.controllers" />


    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" 
      value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    </bean>


</beans>

和控制器

package com.controllers.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/admin")
public class AdminController {

    public AdminController(){
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(){

     return "hello";
    }


    @RequestMapping(value = "/dashbord", method = RequestMethod.GET)
    public String dashbord (){

        return "dashbord";
    }
}

当我导航到http://localhost:8084/PropertySales/admin时 它说404请求的资源不可用。

我检查了服务器日志,我发现了这个

WARNING [http-nio-8084-exec-202] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/PropertySales/admin/dashbord] in DispatcherServlet with name 'dispatcher'

我的代码中出了什么问题。有人可以帮忙吗

4 个答案:

答案 0 :(得分:0)

web.xml中,您没有将dispath-servelet.xml作为contextConfigLocation提供,因此请按以下方式添加:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispath-servelet.xml</param-value>
</context-param>

答案 1 :(得分:0)

我认为您应该在更改名称时将这些添加到您的调度程序servlet:

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-properties.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

答案 2 :(得分:0)

您是否可以尝试将servlet-mapping的web.xml条目修改为以下内容并检查?

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*htm</url-pattern>
</servlet-mapping>

然后使用http://localhost:8084/PropertySales/admin/dashbord.htm

答案 3 :(得分:0)

我找到了自己的解决方案

问题是&#34; applicationContext.xml&#34;文件 什么配置放在&#34; dispatch-servlet.xml&#34;中,应该包含在&#34; applicationContext.xml&#34;

这是我的applicationContext.xml

#include <stdio.h>
#include <string.h>
#include <ctype.h>
void bubblesort(char line[][30], int n);
void swapStrings(char *first[], char *second[]);
void makeUpper(char first[][30], char upperFirst[][30]);

int main()
{
    char state[NUM][30];
    char upper[NUM][30];
    int i, nspot;
    FILE* infile;

    /*Read file into first array.*/
    infile = fopen("sorting.txt", "r");
    if (infile == 0)
    {
        printf("trouble opening file.\n");
        return(0);
    }

    i = 0;
    while ((fgets(state[i], 30, infile)) != NULL)
    {
        nspot = strlen(state[i]) - 1;
        if (state[i][nspot] == '\n')
            state[i][nspot] = '\0'; 
        i++;
    }

    makeUpper(state, upper);

    bubblesort(upper, NUM);

    for (i = 0; i < NUM; i++)
    {
        printf("%s\n", upper[i]);
    }

    fclose(infile);

    return 0;
}


void bubblesort(char line[][30], int n)
{
    int last;
    int i;

    for (last = n - 1; last >= 1; last--)
        for (i = 0; i <= last - 1; i++)
        {
            if (strcmp(line[i], line[i + 1]) > 0)
            swapStrings(&line[i], &line[i + 1]);
        }
}

void swapStrings(char* first[], char* second[])
{
    char swap[30];

    strcpy(swap, first);
    strcpy(first, second);
    strcpy(second, swap);
}

void makeUpper(char first[][30], char upperFirst[][30])
{
    int i,j;
    int test;

    for (i = 0; i < NUM; i++)
    {
        for (j = 0; j < (strlen(first[i])); j++)
        {
            if (first[i][j] > 90)
                strcpy(upperFirst[i][j], toupper(first[i][j]));
            else
                strcpy(upperFirst[i][j], first[i][j]);
        }
        upperFirst[i][strlen(first[i])] = '\0';
    }
}

和Dispatch-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.controllers" />


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    </bean>
</beans>