SSM搭建-Spring MVC搭建第一个HelloWorld(12)

来源:互联网 发布:网络招聘怎么做 编辑:程序博客网 时间:2024/05/08 16:43

本文为博主林炳文Evankaka原创文章 出处http://blog.csdn.net/evankaka

第一个SpringMVC程序

工程免费下载

1、在eclipse中新建立一个web工程,首先将需要的包导入到\WebContent\WEB-INF\lib


2、先配置web.xml

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.     <!-- SpringMVC的前端控制器 -->  
  7.     <servlet>  
  8.         <servlet-name>Hello</servlet-name>  
  9.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  10.         <!-- 设置自己定义的控制器xml文件 -->  
  11.         <init-param>  
  12.             <param-name>contextConfigLocation</param-name>  
  13.             <param-value>/WEB-INF/Hello-servlet.xml</param-value>  
  14.         </init-param>  
  15.         <load-on-startup>1</load-on-startup>  
  16.     </servlet>  
  17.     <!-- Spring MVC配置文件结束 -->  
  18.   
  19.     <!-- 拦截设置 -->  
  20.     <servlet-mapping>  
  21.         <servlet-name>Hello</servlet-name>  
  22.         <!-- 由SpringMVC拦截所有请求 -->  
  23.         <url-pattern>/</url-pattern>  
  24.     </servlet-mapping>  
  25.       
  26. </web-app>
3、配置Hello-servlet.xml:
[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:context="http://www.springframework.org/schema/context"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="    
  5.         http://www.springframework.org/schema/mvc   
  6.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  7.         http://www.springframework.org/schema/beans         
  8.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  9.         http://www.springframework.org/schema/context     
  10.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  11.     <!-- 把标记了@Controller注解的类转换为bean -->  
  12.     <context:component-scan base-package="com.mucfc" />  
  13.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
  14.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
  15.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
  16.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  17.         p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>  
  18.   
  19. </beans>
4、配置控制器:
[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.mucfc;  
  2.   
  3. import org.springframework.stereotype.Controller;    
  4. import org.springframework.ui.ModelMap;    
  5. import org.springframework.web.bind.annotation.RequestMapping;    
  6. import org.springframework.web.bind.annotation.RequestMethod;    
  7. @Controller    
  8. public class HelloWorldController {    
  9.     @RequestMapping(value="/hello",method=RequestMethod.GET,params="userid")    
  10.     public String printWelcome(ModelMap model,String userid) {    
  11.         model.addAttribute("message""Spring 3 MVC Hello World");    
  12.         return "hello";    
  13.     }    
  14. }
5、WEB-INF下新建一个views文件夹,然后再新建一个hello.jsp

[html] view plain copy print?在CODE上查看代码片派生到我的代码片

  1. <%@ page language="java" contentType="text/html; charset=gb2312"  
  2.     pageEncoding="gb2312"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. Message:${message}  
  11. </body>  
  12. </html>
6、整个工程目录如下:

7、直接运行,浏览器中输入http://localhost:8080/SpringMVCLearningChapter1/hello

结果如下:

0 0