我正在通过 Mkyong.com 教程学习 SpringMVC。在他的教程中,Maven 用于构建程序。
我没有使用 Maven 在这个程序中构建它。
我的示例代码:
咖啡.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.xml
<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>
请给我一个 URI(例如:https:\localhost:8080/MVCXML/rest/coffee/arabic)来查看 MkYong.com 教程中的输出(http://www.mkyong.com/spring-mvc/spring- 3-mvc-and-xml-example/ )