IDEA创建一个SpringMVC工程

打开IDEA,new 一个 project

IDEA创建一个SpringMVC工程_第1张图片

创建项目名,设置项目路径

IDEA创建一个SpringMVC工程_第2张图片

然后按照此目录依次创建文件(applicationContext.xml不用管,我们只需要用到dispatcher-servlet.xml

IDEA创建一个SpringMVC工程_第3张图片

web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    //过滤
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
</web-app>

dispatcher-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"
       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.xsd">

    <!--配置自动扫描包-->
    <context:component-scan base-package="com.springmvc"></context:component-scan>
    <!--配置视图解析器,吧handler方法返回解析为实际物理视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置一个前缀-->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <!--配置一个属性-->
        <property name="suffix" value=".html"></property>
    </bean>
</beans>

写一个简单的controller

@Controller
public class helloworldHandler {
    @RequestMapping("/helloworld")
    public  String helloworld(){

        return "success";
    }
}

success.html内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>success page!</h1>
</body>
</html>

index.html内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="helloworld">helloWorld</a>
</body>
</html>

基于tomcat运行:

IDEA创建一个SpringMVC工程_第4张图片

点击helloworld

IDEA创建一个SpringMVC工程_第5张图片

你可能感兴趣的:(Spring,springmvc)