• MyEclipse建立SpringMVC入门HelloWorld项目


    一、首先,建立空的web project项目:

    1.技术分享

    2.技术分享

    3.

     技术分享

    二、其次,导入先关jar包

    1.将jar包导入SpringMVCHelloWorldWebRootWEB-INFlib目录下

     技术分享

    技术分享

    三、接下来修改web.xml文件,在web中,指定我们的DispatcherServlet。(从这里进入SpringMVC的可控范围)。

    1.

     技术分享

    2.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">

      <servlet>

        <servlet-name>dispatcherServlet</servlet-name>

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

        <load-on-startup>1</load-on-startup>

      </servlet>

      <servlet-mapping>

        <servlet-name>dispatcherServlet</servlet-name>

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

      </servlet-mapping>

    </web-app>

    四、添加dispatcherServlet-servlet.xml文件

    1.添加新文件至如下位置

     技术分享

    2. dispatcherServlet-servlet.xml内容如下:

    <?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"

        xsi:schemaLocation="http://www.springframework.org/schema/mvc

            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

            http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

            http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     

        <!-- 自动扫描的包名 -->

        <context:component-scan base-package="com.Ace.controller"></context:component-scan>

        <!-- 默认的注解映射的支持 -->

        <mvc:annotation-driven />

        <!-- 视图解释类 -->

        <bean

            class="org.springframework.web.servlet.view.InternalResourceViewResolver">

            <property name="prefix" value="/WEB-INF/Views/" />

            <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->

            <property name="suffix" value=".jsp" />

        </bean>

     

     

    </beans>

    其中:context:component-scan 指定了要扫描的Controller包。Prefix与suffix指明了路径的前缀与后缀。

    五 添加Controller类。

    1.添加一个Hello.java文件。包名如下:

     技术分享

    2. hello.java内容如下:

    package com.Ace.controller;

     

    import java.util.ArrayList;

    import java.util.List;

    import java.util.Map;

     

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

     

    import org.springframework.stereotype.Controller;

    import org.springframework.ui.Model;

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

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

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

     

    @Controller

    public class Hello {

       

        //hello world

            @RequestMapping(value="/hello")

            public String hello(){

                System.out.println("spring mvc hello world!");

                return "hello";

            }

           

            //hello world

            @RequestMapping(value="/ok")

            @ResponseBody

            public Object ok(){

                System.out.println("ok");

                List<String> list=new ArrayList<String>(); 

                list.add("电视机"); 

                list.add("冰箱"); 

                list.add("山东省"); 

                list.add("多发点"); 

                list.add("D大调"); 

                list.add("规范"); 

                list.add("啦啦啦"); 

                list.add("咯就是"); 

                list.add("阿瓦尔"); 

                return list; 

            }

             

     

    }

    六、 部署

    1.配置在MyEclipse中配置自己安装的Tomcat

     技术分享

    技术分享

    2.发布网站

     技术分享

     技术分享

     技术分享

    七、 启动tomcat,并在浏览器访问

     技术分享

    八、 补充

    1.注意到刚才在Hello.java中的hello方法我们返回到了名为hello的View,结合我们的前后缀配置,我们首先应该建立/WEB-INF/Views/目录

    结果如下

     技术分享

    2.添加hello.jsp文件,结果如下

     技术分享

    内容如下

    <%@ page language="java" contentType="text/html; charset=UTF8"

        pageEncoding="UTF8"%>

    <!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; charset=UTF8">

    <title>Insert title here</title>

    </head>

    <body>

        hello world,gogogo!

    </body>

    </html>

    3.重启tomcat并发布网站后可以浏览如下网页

     技术分享

  • 相关阅读:
    [爬虫] js
    [爬虫] appium-移动端
    如何进行代码的重构
    重写与覆盖的区别
    解决C#中FileSystemWatcher类的Changed事件触发多次的问题
    关于sqlserver 2008 远程导入表数据
    css 选择器
    前端三剑客
    前端的概述
    元类作业
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/6210415.html
Copyright © 2020-2023  润新知