posts - 59, comments - 244, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

spring3.0 mvc和rest风格小例子

Posted on 2010-07-03 19:25 penngo 阅读(33822) 评论(21)  编辑  收藏 所属分类: Java

之前在新一个项目中用了spring3 的mvc开发,用得很爽,不过当时想找些入门的小例子时,找了好久也没找到,

现在写个简单的小例子出来给初学者学习下。
srping3也支持rest,所以例子也包括这部分内容。
先看web.xml配置
<!-- 像js,css,gif等静态文件,需要配置为默认的servlet -->

 <servlet-mapping>  
     
<servlet-name>default</servlet-name>  
     
<url-pattern>*.js</url-pattern>  
 
</servlet-mapping> 
   
<servlet-mapping>  
     
<servlet-name>default</servlet-name>  
     
<url-pattern>*.css</url-pattern>  
 
</servlet-mapping>  

 
<servlet>  
     
<servlet-name>spring</servlet-name>  
     
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
     
<load-on-startup>1</load-on-startup>  
 
</servlet>  
 
<servlet-mapping>  
     
<servlet-name>spring</servlet-name>  
     
<url-pattern>/</url-pattern> 
<!-- url配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的

访问 
--> 
 
</servlet-mapping>  

spring-servlet.xml文件配置,注:XXXX-servlet.xml的XXXX就是上边<servlet-name>spring</servlet-name>中

的名称,如果上边为<servlet-name>mvc</servlet-name>则这个文件名为mvc-servelt.xml。

<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"
>  
          
       
<!-- 自动扫描bean,把作了注解的类转换为bean -->  
      
<context:component-scan base-package="com.mvc.rest" />  
    
       
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
      
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

 
     
       
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->  
       
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
          p:prefix
="/WEB-INF/view/" p:suffix=".jsp" />  
        
       
<bean id="multipartResolver"  
          class
="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
          p:defaultEncoding
="utf-8" />  
           
  
</beans>  

controller类
package com.mvc.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 RestController {
    
    
public RestController(){
        
    }

    
    @RequestMapping(value 
= "/login/{user}", method = RequestMethod.GET)   
    
public ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response,     
            @PathVariable(
"user") String user, ModelMap modelMap) throws Exception {  
        modelMap.put(
"loginUser", user);
        
return new ModelAndView("/login/hello", modelMap);
    }
   
    
     @RequestMapping(value 
= "/welcome", method = RequestMethod.GET)  
        
public String registPost() {  
         
return "/welcome";
        }
  
}

两个jsp页面
\WEB-INF\viewwelcome.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
欢迎
</body>
</html>

\WEB-INF\view\login\hello.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>
你好:<%=request.getAttribute("loginUser") %>
</body>
</html>

运行效果
访问http://localhost:8089/mvc/welcome

http://www.blogjava.net/pengo/

访问http://localhost:8089/mvc/login/pengo

http://www.blogjava.net/pengo/

附件:源码

评论

# re: spring3.0 mvc和rest小例子[未登录]  回复  更多评论   

2010-07-05 11:07 by lveyo
在controller映射方式上像struts2了。

# re: spring3.0 mvc和rest小例子  回复  更多评论   

2010-07-05 18:33 by Jie
留个记号

# re: spring3.0 mvc和rest小例子  回复  更多评论   

2010-07-11 18:19 by yaoneng
期待交流
这是我的Q:1125528760
谢谢!!

# re: spring3.0 mvc和rest小例子  回复  更多评论   

2010-07-17 13:49 by 严维杰
大侠能否告知用“/“和”*.html"的区别哦,用“/"有什么好处。qq:361273693

# re: spring3.0 mvc和rest小例子  回复  更多评论   

2010-08-24 10:20 by jsparrow
这算是什么登陆???让每个登陆的人在地址栏输入用户名密码????????

# re: spring3.0 mvc和rest小例子  回复  更多评论   

2010-08-24 16:11 by zzl
@jsparrow
无语了。


LZ只是在做一个简单的示例而已。跟地址栏输入用户名密码又什么关系啊。

# re: spring3.0 mvc和rest小例子  回复  更多评论   

2010-08-24 22:16 by pengo
@jsparrow

呵呵,只是做个rest的例子,并没有跟用户名密码有关,不知你是怎理解到登陆的。

# re: spring3.0 mvc和rest小例子[未登录]  回复  更多评论   

2010-09-07 22:28 by 刘俊杰
欢迎加入“16023628技术群

# re: spring3.0 mvc和rest小例子  回复  更多评论   

2010-12-13 12:33 by pandora jewelry
其中hsqldb和hibernate都是从jbpm4.4的lib文件夹里面拷过去的

# re: spring3.0 mvc和rest小例子  回复  更多评论   

2011-02-15 10:18 by Chaos
你这根本不是REST

# re: spring3.0 mvc和rest风格小例子  回复  更多评论   

2011-12-31 17:33 by YangJie
困扰我一下午的问题终于解决了。

感谢这位仁兄~~~

# re: spring3.0 mvc和rest风格小例子  回复  更多评论   

2012-03-09 14:04 by bowen.xiao
内容很简单,入门的好例子,谢谢你的分享。
入门经典,一次编译通过

# re: spring3.0 mvc和rest风格小例子[未登录]  回复  更多评论   

2012-04-07 21:54 by aaa
你好,我想问一下为什么我运行不了这个例子呢?

# re: spring3.0 mvc和rest风格小例子[未登录]  回复  更多评论   

2012-04-07 22:09 by aaa
显示的是这个错误!!求楼主及各位高手指教2012-4-7 22:08:43 org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/Spring2012040704/welcome] in DispatcherServlet with name 'spring'

# re: spring3.0 mvc和rest风格小例子  回复  更多评论   

2013-02-26 18:27 by 技术人生
我也运行不了

# re: spring3.0 mvc和rest风格小例子  回复  更多评论   

2013-03-08 19:51 by Iamlonely
@aaa
base-package="com.mvc.rest"
这里的package要是自己的package name

# re: spring3.0 mvc和rest风格小例子  回复  更多评论   

2014-03-03 11:06 by Jove
傻叉啊 。。你知道什么是REST么、。。

# re: spring3.0 mvc和rest风格小例子[未登录]  回复  更多评论   

2014-03-07 18:58 by spring
@Jove
你真有病,博主写的是对的,spring官方文档就是这样写法。自己不懂,就别装b

# re: spring3.0 mvc和rest风格小例子  回复  更多评论   

2014-05-17 17:40 by zuidaima
最代码转载地址:
spring3.0 mvc和rest风格的小例子配置demo代码教程http://www.zuidaima.com/share/1826552160996352.htm

# re: spring3.0 mvc和rest风格小例子  回复  更多评论   

2014-10-30 15:45 by 扎根三
毛线的源码,发布了的,懒得反编译,没意思!!

# re: spring3.0 mvc和rest风格小例子[未登录]  回复  更多评论   

2015-04-27 22:35 by spring
@pengo
学习了,谢谢!

只有注册用户登录后才能发表评论。


网站导航: