1.eclipse-file-new-other:搜索maven,建立一个maven项目,勾选上 Create a simple project点击finish
2.写好Group Id 和Artifact Id ,Packaging选择 war
3.建好工程后会报错,右键Deployment Descriptor:demo2 选择最长的Generate Deployment Descriptor stub ,此时会自动创建web.xml,报错消失。
4.现在可以创建web.xml如下:
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param>(配置spring-mvc.xml的路径很重要,要是配置错误的话,会报错找不到文件的错误) <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
5.然后写好spring-mvc.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- Spring MVC --> <!-- 配置组件扫描 --> <context:component-scan base-package="cn.frode.controller"/> <!-- 配置ViewResolver配置视图解析器 r --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/web/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 注解驱动 --> <mvc:annotation-driven /> </beans>
6.pom.xml--这个pom.xml中有一些是用不到的,基本的都在这里可以参考。
注:
如果没有servlet的jar包会导致jsp页面报错。
如果你的jdk版本是1.8,spring的版本要是4.0以上的,否则不兼容,导致扫描不到包。
<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>cn.frode</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- spring-webmvc 如果是jdk8的话需要用spring4.0以上的版本--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <!-- spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.6.RELEASE</version> </dependency> <!-- servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- junit-单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- mybatis,mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- mysql,commons-dbcp --> <!-- <dependency> <groupId>mysql-connector-java</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!-- jackson * 3 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.3</version> </dependency> <!-- jstl --> <!-- <dependency> <groupId>Jstl</groupId> <artifactId>Jstl</artifactId> <version>1.2</version> </dependency> --> <!-- 消息摘要技术jar包 --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <!-- 文件上传依赖jar包 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.2</version> </dependency> <!-- 文件上传 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2</version> </dependency> </dependencies> </project>
7.jsp文件放置的路径
解决jsp字符集设置的问题:
填写字符集之后点击Update,以后新建的jsp页面都是这个设置的字符集了。
8.controller层--我的项目在web下面有一个hello.jsp页面
package cn.frode.controller; @Controller @RequestMapping("/user")// public class FrodeController { @RequestMapping("/hello") public String hello(){ return "hello"; } }
8.访问成功
默认情况下访问web.xml中的welcome-file的配置页面
通过DispatcherServlet分发给controller在返回给ModelAndView进行解析的页面
附上我的项目的路径
这样就可以将一个简单的项目走通了!