Spring MVC 项目搭建 -1- 创建项目

Spring MVC 项目搭建 -1- 创建项目

1.创建 Dynamic Web project (SpringDemo),添加相关jar包

2.创建一个简单的controller类

package mvc;

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

@Controller
@RequestMapping(value = "/mytest")
public class TestController{
    @RequestMapping(value="/helloSpring",method=RequestMethod.GET)
    public @ResponseBody String HelloWorld(){
        return "Hello Spring";
}

3.创建配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- web 项目名 -->
    <display-name>SpringDemo</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </context-param>
    <!-- 用于初始化自定义配置 context-param-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

    <!--创建默认首页  -->
    <welcome-file-list>
      <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <!--初始化 spring mvc 配置 ,创建一个servlet 映射-->
    <servlet>
        <!-- 配置文件命名为 {servlet-name} + '-servlet.xml',路径为WEB-INF下 -->
        <servlet-name>spring-test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-test</servlet-name>
         <!-- 需要加前缀才可以被该 servlet 拦截  eg: */SpringDemo/test/mytest/login-->
        <url-pattern>/test/*</url-pattern>
    </servlet-mapping>
</web-app>

spring-test-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: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/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">

    <mvc:annotation-driven />
    <!-- 添加 MVC注入配置扫描 -->
    <context:component-scan base-package="mvc" />
</beans>

applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
</beans>

4,在浏览器上调用该controller 或者写一个简单的前端(index.html,只有一个按钮),点击调用后台并且弹出返回内容

index.js

$(function(){
    $('#btn-test').click(function(event){
        test();
    });
});

function test(){
    $.ajax({
        type : "GET",
        url : "./test/mytest/helloSpring",
        success : function(result){
            alert(result)
        },
        error: function(){
            $().toastmessage('showWarningToast', 'Please try again.');
        }
    });
};
上一篇:ELK基础架构解说-运维笔记


下一篇:hive UDF函数