web.xml和springmvc-servlet.xml配置

首先是配置web.xml

将请求交给spring的DispatcherServlet处理
代码如下


    
    
    
    
  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"
  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. <display-name>springmvctest display-name>
  7. <filter>
  8. <filter-name>charsetEncoding filter-name>
  9. <filter-class>org.springframework.web.filter.CharacterEncodingFilter filter-class>
  10. <init-param>
  11. <param-name>encoding param-name>
  12. <param-value>UTF-8 param-value>
  13. init-param>
  14. <init-param>
  15. <param-name>forceEncoding param-name>
  16. <param-value>true param-value>
  17. init-param>
  18. filter>
  19. <filter-mapping>
  20. <filter-name>charsetEncoding filter-name>
  21. <url-pattern>/* url-pattern>
  22. filter-mapping>
  23. <servlet>
  24. <servlet-name>springmvc servlet-name>
  25. <servlet-class>org.springframework.web.servlet.DispatcherServlet servlet-class>
  26. <load-on-startup>1 load-on-startup>
  27. servlet>
  28. <servlet-mapping>
  29. <servlet-name>springmvc servlet-name>
  30. <url-pattern>/ url-pattern>
  31. servlet-mapping>
  32. web-app>

其次配置springmvc-servlet.xml


    
    
    
    
  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. xmlns:mvc = "http://www.springframework.org/schema/mvc"
  7. xmlns:task = "http://www.springframework.org/schema/task"
  8. xsi:schemaLocation = "
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-4.2.xsd
  13. http://www.springframework.org/schema/mvc
  14. http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
  15. http://www.springframework.org/schema/task
  16. http://www.springframework.org/schema/task/spring-task-4.2.xsd" >
  17. <context:component-scan base-package="com.test.controller" >
  18. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  19. context:component-scan>
  20. <mvc:view-controller path="/" view-name="index"/>
  21. <mvc:annotation-driven />
  22. <mvc:resources location="/assets/" mapping="/assets/**"> mvc:resources>
  23. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  24. <property name="prefix" value="/WEB-INF/pages/"/>
  25. <property name="suffix" value=".jsp"/>
  26. bean>
  27. beans>

根据DispatcherServlet的完整路径来看,我们需要加入

spring-core-4.x.x.RELEASE.jar
spring-beans-4.x.x.RELEASE.jar
spring-context-4.x.x.RELEASE.jar
spring-aop-4.x.x.RELEASE.jar
spring-expression-4.x.x.RELEASE.jar
spring-web-4.x.x.RELEASE.jar
spring-webmvc-4.x.x.RELEASE.jar

commons-logging-1.1.3.jar

然后开发Controller

写一个helloworld


    
    
    
    
  1. package com.test.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. @Controller
  7. @RequestMapping(value= "/hello")
  8. public class HelloController {
  9. @RequestMapping(value= "/world",method=RequestMethod.GET)
  10. public String hello (Model model){
  11. model.addAttribute( "msg", "你好spring mvc");
  12. return "index";
  13. }
  14. }

最后开发展示层


    
    
    
    
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding= "UTF-8"%>
  3. "-//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=UTF-8">
  7. <title>hello title>
  8. head>
  9. <body>
  10. <h1>Hello Spring h1>
  11. ${msg }
  12. body>
  13. html>

好了打开浏览器访问
http://localhost:8080/springmvctest/hello/world.html

这里写图片描述

项目结构

这里写图片描述


你可能感兴趣的:(学习)