Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带XML的springmvc_Java_Spring_Spring Mvc_Jaxb_Spring 3 - Fatal编程技术网

Java 带XML的springmvc

Java 带XML的springmvc,java,spring,spring-mvc,jaxb,spring-3,Java,Spring,Spring Mvc,Jaxb,Spring 3,我正在通过Mkyong.com教程学习SpringMVC。在他的教程中,Maven用于构建程序 我不是用Maven在这个程序中构建它 我的示例代码: Coffee.java package com.springMVC; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "coffee") public clas

我正在通过Mkyong.com教程学习SpringMVC。在他的教程中,Maven用于构建程序

我不是用Maven在这个程序中构建它

我的示例代码:

Coffee.java

package com.springMVC;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "coffee")
public class Coffee {

    String name;
    int quanlity;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getQuanlity() {
        return quanlity;
    }

    @XmlElement
    public void setQuanlity(int quanlity) {
        this.quanlity = quanlity;
    }

    public Coffee(String name, int quanlity) {
        this.name = name;
        this.quanlity = quanlity;
    }

    public Coffee() {
    }

}
XMLController.java

package com.springMVC;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.springMVC.Coffee;

@Controller
@RequestMapping("/coffee")
public class XMLController {

    @RequestMapping(value="{name}", method = RequestMethod.GET)
    public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {

        Coffee coffee = new Coffee(name, 100);

        return coffee;

    }
mvc dispacter servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.springMVC" />

    <mvc:annotation-driven />

</beans>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

web.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.springMVC" />

    <mvc:annotation-driven />

</beans>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

SpringWebMVC应用程序
mvc调度器
org.springframework.web.servlet.DispatcherServlet
1.
mvc调度器
/休息/*
上下文配置位置
/WEB-INF/mvc-dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener


请给我一个URI(例如:https:\localhost:8080/MVCXML/rest/coffee/arabic),以便查看MkYong.com tutorial()中的输出

  • xml将带有前缀的URL路由到某个servlet,并且您已经将带有前缀/rest/的所有内容映射到spring DispatcherServlet
  • 控制器路由由
    @controller
    注释的参数处理,您已经用
    /coffee
  • 最后还有一个变量,它是咖啡的名称,它被放在
    @PathVariable
    所以在那里我看不到
    /MVCXML/
    。mkyong.com没有提供web.xml,所以如果您还不知道web.xml和Spring的dispatcher servlet是如何协同工作的,那么前缀应该是什么就有点不清楚了


    正确的URI应该是
    http://localhost:8080/rest/coffee/arabic

    以下是URL映射的工作原理:

  • xml将带有前缀的URL路由到某个servlet,并且您已经将带有前缀/rest/的所有内容映射到spring DispatcherServlet
  • 控制器路由由
    @controller
    注释的参数处理,您已经用
    /coffee
  • 最后还有一个变量,它是咖啡的名称,它被放在
    @PathVariable
    所以在那里我看不到
    /MVCXML/
    。mkyong.com没有提供web.xml,所以如果您还不知道web.xml和Spring的dispatcher servlet是如何协同工作的,那么前缀应该是什么就有点不清楚了


    正确的URI应该是
    http://localhost:8080/rest/coffee/arabic

    在mkyong.com上你能学到的唯一一件事就是如何欺骗搜索引擎将你的网站推到搜索结果的顶端。转到VMWare站点并遵循Spring人员提供的Spring MVC教程。您的MVC-dispatcher-servlet.xml在
    上下文:component scan
    @Kevin中有错误的包名-现在我更改了包名,但遇到了同样的问题。当您在jetty/tomcat下启动应用程序时,日志显示了什么?如果点击
    http://localhost:8080/rest/coffee/arabic
    ?您是否在这两点的日志中看到异常?我不清楚您的确切问题是什么。@Kevin错误是“在名为“mvc dispatcher”的DispatcherServlet中找不到URI为[/MVCXML/coffee]的HTTP请求的映射”。在mkyong.com上,您可以学到的唯一一件事是如何欺骗搜索引擎将您的站点推到搜索结果的顶部。转到VMWare站点并遵循Spring人员提供的Spring MVC教程。您的MVC-dispatcher-servlet.xml在
    上下文:component scan
    @Kevin中有错误的包名-现在我更改了包名,但遇到了同样的问题。当您在jetty/tomcat下启动应用程序时,日志显示了什么?如果点击
    http://localhost:8080/rest/coffee/arabic
    ?您是否在这两点的日志中看到异常?我不清楚您的确切问题是什么。@Kevin错误是“在名为“mvc dispatcher”的DispatcherServlet中找不到URI为[/MVCXML/coffee]的HTTP请求的映射”