• Spring MVC入门实例


    1.web.xml配置

    <?xml version="1.0" encoding="UTF-8"?

    >

    <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>myweb</display-name>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>
     
    <!-- 载入全部的配置文件 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/spring-*.xml</param-value>
    </context-param>
     
    <!-- 配置Spring监听 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     
    <!-- 配置字符集 -->
    <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/</url-pattern>
    </filter-mapping>
     
     
    <!-- 配置SpringMVC -->
    <servlet>
    <description>myweb</description>
    <display-name>myweb</display-name>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:config/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
     
    <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
     
    </web-app>

    2.spring-mvc配置

    <?

    xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
    <!-- 开启Spring MVC注解 -->
    <mvc:annotation-driven />
    <!-- 处理静态资源 -->
    <mvc:resources location="/resources" mapping="/resources/**" />
     
    <!-- 打开Spring的Annotation支持 -->
    <context:annotation-config />
     
    <!-- 注解扫描包 -->
    <context:component-scan base-package="com.myweb.controller" />
     
     
    <!-- ViewResolver -->
    <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
    <bean id="defaultViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    </bean>
     
     
    </beans>

    3.HelloController类

    package com.myweb.controller;
     
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
     
    @Controller
    public class HelloController {
     
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView printWelcome() {
    ModelAndView mv = new ModelAndView();
    mv.setViewName("welcome");
    mv.addObject("message", "you are welcome");
    return mv;
    }
     
    }


    4.welcome.jsp


    <%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ${message}
    </body>
    </html>

    5.eclipse的文件夹结构图



    6.訪问http://localhost:8080/myweb/hello。输出:

    you are welcome



  • 相关阅读:
    微信开发:消息回复总结
    *** wechat-php-sdk 微信公众平台php开发包
    **微信接入探秘(一)——从零认识微信接口(主动接口和被动接口)
    《30天自制操作系统》笔记(01)——hello bitzhuwei’s OS!【转】
    Linux进程调度原理【转】
    Linux进程核心调度器之主调度器schedule--Linux进程的管理与调度(十九)【转】
    Tslib触摸屏官网【转】
    Tslib的移植【转】
    Linux Kernel代码艺术——数组初始化【转】
    Linux 内核进程管理之进程ID【转】
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6850575.html
Copyright © 2020-2023  润新知