springMVC认识(一)
2017-07-17 本文已影响0人
又是那一片天
先来看看springMVC流程图:
图一优点百度....
向工程中添加jar包:
jar工程wed.xml配置:
<!-- 配置SpringMVC springDispatcherServlet-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springDispatcherServlet的初始化参数:配置SpringMVC配置文件的位置名称-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 加载创建-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 拦截的url -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
创建SpringMVC配置文件设置视图解析器
<?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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 配置扫描包:带有spring特殊注解并赋予相应的功能 -->
<context:component-scan base-package="chen"></context:component-scan>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
创建控制器以及url映射
@Controller
public class HelloWorld {
/**
*1 使用@RequestMapping注解映射url
*2 返回值通过视图解析器解析为实际的物理视图
* InternalResourceViewResolver
* prefixֵ值+返回值+suffixֵ值= /WEB-INF/views/one.jsp 然后转发
* */
@RequestMapping("/test")
public String hello() {
System.out.print("服务器接受");
return "one";
}
}
这就完成了springMVC初步使用