Intellij搭建springmvc常见问题解决方案
程序员文章站
2022-07-05 09:39:59
注意是maven的webapp:选择maven下一步下一步。maven下载过慢在setting中加入镜像。 我也有疑问这是什么鬼格式,但是证明,格式不用调整,直接粘贴进去:...
注意是maven的webapp:
选择maven下一步下一步。
maven下载过慢在setting中加入镜像。 我也有疑问这是什么鬼格式,但是证明,格式不用调整,直接粘贴进去:
<mirror> <id> nexus-aliyun </id> <mirrorof> * </mirrorof> <name> nexus aliyun </name> <url> http://maven.aliyun.com/nexus/content/groups/public </url> </mirror>
我在这里踩了一个特郁闷的坑,注意看这里,没有package war, 这里有毒,导致我的tomcat一直加不进去artifacts
暂时就是这个鬼样子的结构,手动补全结构,
调整一下包:
加入和pom中的依赖 和 web.xml和 springmvc.xml
注意这里别丢了 pom:
pom.xml:
<?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.shishi</groupid> <artifactid>mymvc</artifactid> <version>1.0-snapshot</version> <packaging>war</packaging> <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> <spring-version>5.2.8.release</spring-version> </properties> <dependencies> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring-version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>5.2.8.release</version> </dependency> <dependency> <groupid>commons-logging</groupid> <artifactid>commons-logging</artifactid> <version>1.2</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>4.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>1.1.1</version> </dependency> <dependency> <groupid>taglibs</groupid> <artifactid>standard</artifactid> <version>1.1.1</version> </dependency> </dependencies> </project>
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>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
springmvc.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/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 http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.springmvc"></context:component-scan> <bean id="internalresourceviewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--能访问到静态资源,但是url和控制器中对应的方法没有映射关系--> <mvc:default-servlet-handler/> <mvc:annotation-driven></mvc:annotation-driven> </beans>
这已经是很精简版的了。
配置tomcat, 我在这里遇到了问题,记录详细点:
(上面删除了mymvc:war也是可以的。图就懒得替换了。)
ok
启动tomcat成功:
而且注意这里不要用 去试,会404.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。