本文章主要参考七小栈主的文章,特此说明:
前提
在配置好JDK,配置好Maven,配置链接如下:如何配置Java环境,包括JDK,Maven等
建项目
IEDA中File->New->Project->Maven->填写好GroupId,ArtifactId等,然后选择刚才配置的Maven的目录和目录下settings.xml和本地存放架包的路径(这个一般在C:\Users\{用户名}\.m2\repository中),起个项目的名字,点击Finish项目就建好了
配置pom.xml
登录http://mvnrepository.com/,可查看架包配置,直接拷贝到dependencies下即会自动Down下来
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Jef</groupId> <artifactId>me</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>me Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.1.RELEASE</version> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.1.RELEASE</version> </dependency> <!--Spring Web + Spring MVC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.1.RELEASE</version> </dependency> <!--json支持--> <!-- https://mvnrepository.com/artifact/org./json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20160212</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph --> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <!--json serialize and deserialization--> <!-- 引入fastjson依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.12</version> </dependency> <!-- 引入gson依赖 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> <!--NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config--> <!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>me</finalName> </build> </project>
配置web.xml
项目初始运行的时候就会加载web.xml,所以web.xml的配置尤其重要,src->main->webapp->WEB-INF->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" version="3.0"> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- Spring MVC配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param>--> <!-- load-on-startup元素标记容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法) --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> <!--<url-pattern>*.do</url-pattern>--> </servlet-mapping> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
新建 spring-servlet.xml
在WEB-INF文件夹下新建 spring-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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> <mvc:annotation-driven > </mvc:annotation-driven> <!-- 启动包扫描功能,以便注册带有@Controller、@service、@repository、@Component等注解的类成为spring的bean --> <context:component-scan base-package="HelloSpringMvc.controller" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/"/> <!-- 前缀 --> <property name="suffix" value=".jsp"/> <!-- 后缀 --> </bean> <!-- 访问静态文件(jpg,js,css)的方法 --> <mvc:resources location="/files/" mapping="/files/**" /> <mvc:resources location="/scripts/" mapping="/scripts/**" /> <mvc:resources location="/styles/" mapping="/styles/**" /> <mvc:resources location="/views/" mapping="/Views/**" /> </beans>
这里我们配置了静态文件路径,因此在目录结构新建几个文件夹用以放静态文件例如脚本,图片文件,视图之类的。
新建applicationContext.xml
在Resources资源文件夹下新建applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 我们可以在其中添加我们所需要配置的bean,也可以添加相应的数据库连接和事务处理等等,方便后续拓展 --> </beans>
测试SpringMVC配置
TestController.java
package HelloSpringMvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by Jef on 2016/8/29. * please remember Jef`s page -> http://www.cnblogs.com/tufujie * thx~ */ @Controller @RequestMapping(value = "/Test/*") //访问的url地址前缀,可以不写,写了就必须在方法url前面先加上class url 进行区分控制器 public class TestController { //访问地址:http://localhost:8080/Test/returnSuccess @RequestMapping(value = "returnSuccess") //实际访问的url地址 public String returnSuccess() { return "/views/success"; //返回Views文件夹下的success.jsp页面 } //访问地址:http://localhost:8080/Test/returnString @RequestMapping(value = "returnString", produces = {"text/plain;charset=UTF-8"}) //produces用于解决返回中文乱码问题,application/json;为json解决中文乱码 @ResponseBody //用于返回字符串,不写即返回视图 public String returnString() { return "hello return string 这是中文,并没有乱码"; } }
新建一个success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> Success ! hello Jef! </body> </html>
开始试运行
然后下面的图标任意一个都可以开始运行
http://localhost:8080/Test/returnSuccess
http://localhost:8080/Test/returnString
时间: 2024-10-18 08:54:15