idea开发spring整合mvc的web小项目配置详情
新建一个项目,这个项目和eclipse中的workspaces差不多。file>new>project
选择empty project
项目名称
弹出下面页面
加号——new module
选择maven>create from archetype 勾上,选择webapp
取项目名称
maven配置本地maven
点击ok
main目录下新建java和resources目录,java右键单击选择make directory as > sources root
resources右键单击选择make directory as>resources root
配置pom.xml 配置依赖
代码:<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.17.RELEASE</version> </dependency>
配置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_2_5.xsd"
id="jt-manage" version="2.5">
<display-name>jt-manage</display-name>
<display-name>Archetype Created Web Application</display-name>
<!--定义前端控制器 servlet第一次请求时才会加载启动 懒加载的方式 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--扫描全部以applicationContext开通的配置文件 -->
<param-value>classpath:applicationContent-mvc.xml</param-value>
</init-param>
<!--让servlet第一个加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>characterEncoding</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>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
配置spring配置文件:applicationContent-mvc.xml,放在resources目录下。
<?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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.pro.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--定义前缀和后缀 -->
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:default-servlet-handler/>
</beans>
控制层:
package com.pro.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/user")
public String user(){
return "user";
}
}
web-inf下面新建views目录下放着user.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
user user 2222diyer yuser2
</body>
</html>
完成一个spring整合mvc的web项目: