zoukankan      html  css  js  c++  java
  • springMVC.helloworld

    ·Spring Web MVC  轻量级Web框架,帮助简化开发

    创建maven工程

    1、添加jar包(junit, javax.servlet-api, commons-logging, log4j, spring-core, spring-context, spring-context-support, spring-webmvc, spring-web)

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.zzxt</groupId>
      <artifactId>springMVC.helloworld</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <name>springMVC.helloworld Maven Webapp</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <spring.version>4.3.10.RELEASE</spring.version>
      </properties>
    
      <dependencies>
       
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
        
       
      </dependencies>
    
      <build>
        <finalName>springMVC.helloworld</finalName>
       
      </build>
    </project>

    2、配置文件

    <?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:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd ">
    <context:component-scan base-package="com.zzxt(此处为applicationContext-mvc.xml所在包名)"></context:component-scan>
    </beans>

    3、配置web.xml

    <!DOCTYPE web-app PUBLIC

     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

     "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app>

    <servlet>

    <servlet-name>DispatcherServlet</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:config/applicationContext-mvc.xml</param-value>

    </init-param>

    </servlet>

    <!-- 

    / :拦截所有的请求(包含静态文件、不包含jsp)

    *.action *.do (开一个后台系统的推荐配置)

    /* :拦截所有的请求(包含静态文件、jsp    )

    -->

    <servlet-mapping>

    <servlet-name>DispatcherServlet</servlet-name>

    <url-pattern>/</url-pattern>

    </servlet-mapping>

    </web-app>

    4、Controller

    package com.zzxt;

    import org.apache.commons.logging.Log;

    import org.apache.commons.logging.LogFactory;

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.servlet.ModelAndView;

    @Controller(表明是一个控制层)

    public class HelloWorldController {

    private Log log = LogFactory.getLog(HelloWorldController.class);

    @RequestMapping("sayHello")

    public ModelAndView(以ModelAndView的形式返回) sayHello(){

    ModelAndView mav =new ModelAndView();(返回ModelAndView,新建一个ModelAndView对象)

    mav.setViewName("views/success.jsp");(返回视图)

    log.info("--------spring MVC hello world!");

    return mav;

    }

    }

  • 相关阅读:
    使用golang访问kubebernetes
    使用 Rancher 管理现有 Kubernetes 集群
    Running powershell scripts during nuget package installation and removal
    How to Create, Use, and Debug .NET application Crash Dumps in 2019
    寻找写代码感觉(一)之使用 Spring Boot 快速搭建项目
    Selenium+Java之解决org.openqa.selenium.InvalidArgumentException: invalid argument报错问题
    Selenium环境搭建
    关于Xpath定位方法知道这些基本够用
    Web自动化之浏览器启动
    【翻译】编写代码注释的最佳实践
  • 原文地址:https://www.cnblogs.com/zshibo/p/8858484.html
Copyright © 2011-2022 走看看