目录结构:
lib里面有jar包是因为该Maven工程创建了之后又添加了 Spring MVC 框架支持,idea自动导入的jar包。
关键文件的配置
1. pox.xml (Maven项目)
<?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>sfwn</groupId> <artifactId>springmvc</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> </dependencies> </project>
2.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_3_1.xsd" version="3.1"> <!-- 加载Spring配置文件 --> <!--<context-param>--> <!--<param-name>contextConfigLocation</param-name>--> <!--<param-value>/WEB-INF/applicationContext.xml</param-value>--> <!--</context-param>--> <!--<listener>--> <!--<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>--> <!--</listener>--> <!-- 加载spring mvc --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.dispatcher-servlet.xml (与 web.xml 中制定的 servlet-name 对应)
注:applicationContext.xml 文件好像可以不定义,具体的还不明白,留给以后学习
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- HandlerMapping --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- HandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- ViewResolver --> <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:annotation-driven /> <!-- 处理器 --> <!-- <bean name="/hello" class="com.mvc.jn.controller.HelloWorldController"/> --> <context:component-scan base-package="lj.controllers"/> </beans>
4.Controller
package lj.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.portlet.bind.annotation.RenderMapping; /** * Created by Sfwn on 2015/9/10. */ @Controller public class HelloController { @RequestMapping({"/hello", "lj", "/"}) public String hello() { System.out.println("hello"); return "hello"; } }
其他配置:
1.tomcat配置
1.1 先在Deployment(部署)标签下按+号添加 Artifacts , Application context 为 对应的 /springmvc
注:Application context 是什么我还不明白
1.2 修改 On Upda action 和 On frame deactivation 为下图的选项,只有在1.1里先设置Deployment标签之后这两个选项框里才能选择Update classes and resources
检测:
打开浏览器:输入
//三者都能访问,第二个 <span style="font-family: Arial, Helvetica, sans-serif;">localhost:8080/springmvc </span>要访问的话得先删除默认的index.jsp localhost:8080/springmvc/hello localhost:8080/springmvc localhost:8080/springmvc/lj
小结:
1.配置文件的配置是关键,其中xmlns的配置很重要,有时间需要学习一下xml命名空间;
2.jar包依赖如果可以都用maven来管理,如果一下maven一下本地添加,jar包重复添加的话,tomcat可能会报错;如果真的报错了,一定要学会自己看日志文件,caused by!然后查阅资料进行解决,知道是什么问题的话网上绝大部分都有答案;
3.心情烦躁的时候,最好休息一下,做点别的事情,不要死磕,从头到尾好好整理思路,会恍然大悟。