springMVC(二)web.xml和springmvc-servlet.xml配置

首先是配置web.xml

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


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>springmvctestdisplay-name>
    
    <filter>
        <filter-name>charsetEncodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>charsetEncodingfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

其次配置springmvc-servlet.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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-4.2.xsd">

    
    <context:component-scan base-package="com.test.controller" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>

    
    <mvc:view-controller path="/" view-name="index"/>

    
    <mvc:annotation-driven />

    
    <mvc:resources location="/assets/" mapping="/assets/**">mvc:resources>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    bean>

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

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/hello")
public class HelloController {
    @RequestMapping(value="/world",method=RequestMethod.GET)
    public String hello(Model model){
        model.addAttribute("msg", "你好spring mvc");
        return "index";
    }
}

最后开发展示层

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hellotitle>
head>
<body>
<h1>Hello Springh1>
${msg }
body>
html>

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

这里写图片描述

项目结构

这里写图片描述


你可能感兴趣的:(Java基础)