Spring MVC 入门示例

来源:互联网 发布:k线训练软件 编辑:程序博客网 时间:2024/04/27 14:46

Spring MVC 入门示例

    博客分类: 
  • SSH
springmvcweb控制器 

为了简单,将spring-framework中dist下的所有jar包拷贝到项目的WEB-INF/lib目录下
需要添加Apache commons logging日志,此处使用的是commons.logging

 

web.xml中添加如下配置:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <welcome-file-list>  
  7.         <welcome-file>index.jsp</welcome-file>  
  8.     </welcome-file-list>  
  9.     <session-config>  
  10.         <session-timeout>30</session-timeout>  
  11.     </session-config>  
  12.     <filter>  
  13.         <filter-name>CharacterEncodingFilter</filter-name>  
  14.         <filter-class>  
  15.             org.springframework.web.filter.CharacterEncodingFilter  
  16.         </filter-class>  
  17.         <init-param>  
  18.             <param-name>encoding</param-name>  
  19.             <param-value>utf-8</param-value>  
  20.         </init-param>  
  21.         <init-param>  
  22.             <param-name>forceEncoding</param-name>  
  23.             <param-value>true</param-value>  
  24.         </init-param>  
  25.     </filter>  
  26.     <filter-mapping>  
  27.         <filter-name>CharacterEncodingFilter</filter-name>  
  28.         <url-pattern>/*</url-pattern>  
  29.     </filter-mapping>  
  30.     <servlet>  
  31.         <servlet-name>dispatcherServlet</servlet-name>  
  32.         <servlet-class>  
  33.             org.springframework.web.servlet.DispatcherServlet  
  34.         </servlet-class>  
  35.         <init-param>  
  36.             <param-name>contextConfigLocation</param-name>  
  37.             <param-value>/WEB-INF/spring/*.xml</param-value>  
  38.         </init-param>  
  39.         <load-on-startup>1</load-on-startup>  
  40.     </servlet>  
  41.     <servlet-mapping>  
  42.         <servlet-name>dispatcherServlet</servlet-name>  
  43.         <url-pattern>*.do</url-pattern>  
  44.     </servlet-mapping>  
  45. </web-app>  

以上配置已经增加了由spring提供的编码过滤器来解决乱码问题

 

编写MVC控制配置文件mvc-config.xml,该文件在WEB-INF的spring文件下,而web.xml中已经配置加载该文件夹下的所有xml文件

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context.xsd">  
  11.   
  12.     <!-- prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀)  
  13.         比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp” -->  
  14.     <bean  
  15.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  16.         <property name="prefix" value="/jsp/" />  
  17.         <property name="suffix" value=".jsp" />  
  18.     </bean>  
  19.   
  20.     <bean name="/hello.do" class="test.HelloWorldController" />  
  21. </beans>  

 

编写控制的类,该类接受参数,设置该参数为视图数据

Java代码  收藏代码
  1. package test;  
  2. import javax.servlet.http.HttpServletRequest;  
  3. import javax.servlet.http.HttpServletResponse;  
  4. import org.springframework.web.servlet.ModelAndView;  
  5. import org.springframework.web.servlet.mvc.Controller;  
  6. // http://localhost:8080/spring/hello.do?user=java  
  7. public class HelloWorldController implements Controller{  
  8.     public ModelAndView handleRequest(HttpServletRequest request,  
  9.             HttpServletResponse response) {  
  10.         ModelAndView mv = new ModelAndView();  
  11.         // 添加模型数据 可以是任意的POJO对象  
  12.         mv.addObject("user", request.getParameter("user"));  
  13.         // 设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面  
  14.         mv.setViewName("hello");  
  15.         return mv;  
  16.     }  
  17. }  

 

视图使用JSP,页面很简单。在WebRoot下新建文件夹jsp,新建jsp命名为hello.jsp。

Java代码  收藏代码
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <html>  
  3. <head>  
  4. <title>SpringMVC</title>  
  5. </head>  
  6. <body>  
  7. 您好,${user }!  
  8. </body>  
  9. </html>  

 

访问路径http://localhost:8080/spring/hello.do?user=java看效果

 

请您到ITEYE网站看 java小强 原创,谢谢!

http://cuisuqiang.iteye.com/ 

自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!

2 
3 
分享到:  
Spring MVC Controller配置方式 | Spring 发送邮件 使用File指定附件
  • 2014-04-10 13:02
  • 浏览 5278
  • 评论(2)
  • 分类:编程语言
  • 查看更多
相关资源推荐
  • AtCommands相关学习资料(中文+英文)
  • At Commands:从白痴到大师的修炼历程(一)
  • Delphi7高级应用开发随书源码
  • 聊天机器人2002
  • LTE AT指令
  • 迅捷全站功能模块
  • 封装了DAO对象用于直接操纵access数据库 
  • MVC 编程语言
  • awk编程语言入门
  • 从C到嵌入式C编程语言:入门·实用·深入
  • 嵌入式c编程语言入门与深入
  • Apple Swift 编程语言入门教程
  • Apple Swift编程语言中文入门教程
  • 苹果Swift编程语言入门教程
  • 苹果Swift编程语言入门教程【中文版】
  • Apple Swift编程语言入门教程
  • 苹果Swift编程语言入门教程
  • 苹果Swift编程语言入门教程【中文版】.pdf
  • Apple Swift编程语言入门教程
  • Apple_Swift编程语言入门教程
参考知识库
Java EE知识库23262  关注 | 1416  收录
评论
2 楼 Stark_Summer 2014-05-10  
endual 写道
果断用maven啊。

果断用 maven too
1 楼 endual 2014-04-10  
果断用maven啊。
原创粉丝点击