Idea创建一个springmvc项目

1.新建一个springmvc项目(如果没有spring选项请参考)

https://blog..net/as_64/article/details/122960382?spm=1001.2014.3001.5501

2.修改web.xml配置文件,将servelet-mapping里面的内容修改为(拦截所有请求)

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

3.修改dispatch-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 https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置自定义扫描包,将base-package修改为你的包名-->
    <context:component-scan base-package="com.zstu.springmvc"></context:component-scan>
    <!--配置视图解析器:把handler方法返回值解析为实际的物理视图,如返回值为sucess,物理视图为prefix+sucess+.jsp-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

4.创建一个handdler 5.修改index.jsp,并在web-inf下新建一个views文件夹,新建sucess.jsp index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>

  </head>
  <body>
  <a href="helloword">helloword</a>
  $END$
  </body>
</html>

sucess.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <h4>Sucess page</h4>
</head>
<body>

</body>
</html>

6.配置tomcat服务器 6.1在run这里选择编辑 6.2选择tomcat-local-修改端口号为8088(8080会冲突) 6.3选择server旁边的deployment 6.4此时运行可能会报错

org.apache.catalina.core.StandardContext.startInternal One or more listeners
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
经验分享 程序员 微信小程序 职场和发展