SpringMVC的环境搭建
SpringMVC的环境搭建
SpringMVC的原理图:
springMVC是基于Spring的 所以要导入spring的包:
aopalliance
commons-loggging
spring-aop
spring-beans
spring-context
spring-core
spring-expression
aopalliance
commons-loggging
spring-aop
spring-beans
spring-context
spring-core
spring-expression
要用springMVC要引入
spring-web
spring-webmvc
spring-web
spring-webmvc
用到jsp jstl就要假如jstl的包和serlvet的
引入
javax.servlet-api-3.0.1
jstl-1.2
最终要引入的所有包:
记得要手动把包引入web项目!!
1.在resource包右键new XML Configuration File 选SpringConfig 创建spring-mvc.xml
还要更改XML约束为
<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/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-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
还要更改XML约束为
<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/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-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<?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/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-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--开启springmvc的注解驱动-->
<mvc:annotation-driven/>
<!--配置包扫描-->
<context:component-scan base-package="com.dengweiquan.**" />
<mvc:annotation-driven/>
<!--配置包扫描-->
<context:component-scan base-package="com.dengweiquan.**" />
<!--配置viewResolver-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--getrequestdispature的时候要填的前缀 和后准都自动填好-->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
<!--jsp要用到jstl-->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
</beans>
2.在web.xml里面配置DispatherServlet
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">
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>ssm-demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--声明spring-mvc配置所在位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:./com/dengweiquan/resource/spring-mvc.xml</param-value>
</init-param>
<!--设置工程启动之后就立马加载这个servlet 设置为优先级最高-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm-demo</servlet-name>
<!--斜杠拦截所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
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">
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>ssm-demo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--声明spring-mvc配置所在位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:./com/dengweiquan/resource/spring-mvc.xml</param-value>
</init-param>
<!--设置工程启动之后就立马加载这个servlet 设置为优先级最高-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm-demo</servlet-name>
<!--斜杠拦截所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.创建实体类User:
public class User {
private int id;
private String name;
private String name;
public int getId() {
return id;
}
return id;
}
public void setId(int id) {
this.id = id;
}
this.id = id;
}
public String getName() {
return name;
}
return name;
}
public void setName(String name) {
this.name = name;
}
}
this.name = name;
}
}
4.创建UseConroller类
//打了@Controller 都归springMVC管
//打了@Controller 是写请求路径的 假如请求路径里面是带user的 例如user/test.jsp 或者user/demo.jsp 这个请求就会分发到这个controller来处理
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("/test")
public String testUser(){
public String testUser(){
//返回userspringmvc自动帮你加好了前后缀 就是我们要去的user.jsp 在viewResolver哪里已经配置好了路径的前后缀
return "user";
}
@RequestMapping("/demo")
public String demoUser(){
//返回userspringmvc自动帮你加好了前后缀 就是我们要去的user.jsp 在viewResolver哪里已经配置好了路径的前后缀
return "demo";
}
}
5.在WEB-INF目录下创建view目录 再创建demo.jsp 和user.jsp
demo.jsp :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
这里是demo.jsp
</body>
</html>
<html>
<head>
<title>Title</title>
</head>
<body>
这里是demo.jsp
</body>
</html>
user.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
这里是user.jsp
</body>
</html>
<html>
<head>
<title>Title</title>
</head>
<body>
这里是user.jsp
</body>
</html>
项目目录结构:
启动项目 然后输入网址 http://localhost:8080/user/test 页面会显示这里是user.jsp
demo也同理
SpringMVC搭建完毕 可喜可贺O(∩_∩)O