`

初试Spring3 MVC REST

阅读更多

文澜阁社区 

Spring的版本:3.0.3

需要的包:

org.springframework.asm-3.0.3.RELEASE.jar

org.springframework.beans-3.0.3.RELEASE.jar

org.springframework.context-3.0.3.RELEASE.jar

org.springframework.core-3.0.3.RELEASE.jar

org.springframework.exception-3.0.3.RELEASE.jar

org.springframework.web-3.0.3.RELEASE.jar

org.springframework.web.servlet-3.0.3.RELEASE.jar

commons-fileupload-1.2.1.jar

commons-logging-1.1.1.jar

 

 

所有文件

Demo

|-src

|   |-demo

|         |-springmvc

|                 |-rest

|                      |-RestDemo.java

|-WebRoot

      |-view

      |    |-welcome.jsp

      |    |-login

      |         |-login.jsp

      |-WEB-INF

           |-lib

           |-demo-servlet.xml

           |-web.xml

 

 

/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <!-- Spring MVC, start with DispatcherServlet
  the default context location : /WEB-INF/{servlet-name}-servlet.xml -->
 <servlet>
  <servlet-name>demo</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>demo</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>
 

/WEB-INF/demo-servlet.xml

<?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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 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">
 
 <!-- Auto scan, declare the location path --> 
 <context:component-scan base-package="demo.springmvc.rest" /> 

 <!-- Using annontation --> 
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 
 <!-- Resolve the view, declare the prefix and suffix --> 
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
  p:prefix="/view/" p:suffix=".jsp" /> 
 
 <bean id="multipartResolver" 
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver" 
  p:defaultEncoding="utf-8" />
</beans>
 
demo.springmvc.rest.RestDemo.java

package demo.springmvc.rest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.servlet.ModelAndView;

@Controller
public class RestDemo {
 public RestDemo(){}
 
 @RequestMapping(value="/login/{user}", method=RequestMethod.GET)
 public ModelAndView demo(HttpServletRequest request, HttpServletResponse response,
   @PathVariable("user") String user, ModelMap modelMap) throws Exception{
  modelMap.put("user", user);
  return new ModelAndView("/login/hello", modelMap);
 }
 
 @RequestMapping(value="/welcome", method=RequestMethod.GET)
 public String welcome(){
  return "/welcome";
 }
}
 /view/login/hello.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
   
    <title>My JSP 'hello.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    Hello, ${user}
  </body>
</html>
 /view/welcome.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
   
    <title>My JSP 'welcome.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    Welcome
  </body>
</html>
 

访问页面和返回结果

http://localhost:8080/demo/login/Ben

-------------------------------------

Hello, Ben

 

http://localhost:8080/demo/welcome

----------------------------

Welcome

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics