Myeclipse通过maven搭建SpringMvc框架(二)
突然想起来大家可能没有安装maven,不过这个我就不写了,百度一下,你就会了,咱们继续搭建SpringMvc框架。
一、配置web.xml文件
既然我们是web程序,那么web.xml文件是必备的,maven创建的项目不知道为什么没有,我们就自己动手丰衣足食吧
自己建个文件夹,然后自己建个web.xml,下面是代码,配置前端控制器代码里面会有spring-mvc配置文件的路径,大家注意一下下,它会在resource文件下寻找配置文件,大家参考一下,配置文件在下面讲,不过创建配置文件大家就得自己建了
<?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" id="WebApp_ID" version="3.0">
<display-name>prince</display-name>
<!--配置前端控制器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml,classpath:spring/spring-view-controller.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 过滤器,不是很关键 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 似乎是web.xml自带的 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/page/404.html</location>
</error-page>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
二、配置文件内容
spring-mvc.xml
这个配置文件三个简单配置即可,不过我原来使用的是复杂的配置,就全部写上了 ,在这里大家可以了解一下springMvc的流程,当然我也是为了自己记录一下,大家有兴趣的话最好上网找找
- 发送请求到前端控制器DispatcherServlet
- 前端控制器请求HandlerMapping查找Handler,可以根据xml配置或注解进行查找
- 处理器映射器HandlerMapping向前端控制器返回Handler
- 前端控制器调用处理器适配器去执行Handler
- 处理器适配器执行Handler
- Handler执行完成后向前端控制器返回ModelAndView
- 处理器适配器向前端控制器返回ModelAndView
- 前端控制器请求视图解析器去进行视图解析,根据逻辑视图名解析成真正的视图
- 视图解析器向前端控制器返回View
- 前端控制器进行视图渲染,视图渲染将模型数据填充到request域
- 前端控制器向用户响应结果
<?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:p="http://www.springframework.org/schema/p"
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-4.0.xsd">
<!-- 配置扫描的包 -->
<context:component-scan base-package="controller"/>
<!--表示使用注解驱动SpringMvc-->
<mvc:annotation-driven/>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/page/" />
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<!--以下配置可以省略,上面是springmvc快速配置,省略了其中handlermapping和handlerAdpter的配置-->
<!--由于已经配置上面的context:componet-scan,可以省略此配置,使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<!-- <context:annotation-config/> -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> -->
<!-- <bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean> -->
<!--避免 IE 执行 AJAX 时,返回 JSON 出现下载文件 -->
<!-- <bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean> -->
<!-- 启动 SpringMVC 的注解功能,完成请求和注解 POJO 的映射 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter" />
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean> -->
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" >
<property name="maxUploadSize">
<value>10485760</value>
</property>
</bean>
</beans>
spring-view-controller.xml
一般springmvc都是经过Controller,但是当我们不想经过Controller,而是直接访问视图的时候。就可以通过mvc:view-controller。
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:view-controller path="/image/news" view-name="redirect:http://localhost/image/news"/>
<mvc:resources location="/image/" mapping="/image/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/page/" mapping="/page/**"/>
<mvc:resources location="/video/" mapping="/video/**"/>
</beans>
三、创建Controller类
四、创建HTML文件
启动服务器,然后到浏览器中查看
这是通过Controller返回的页面
这是直接访问的页面
不行了,我太累了,具体的我也写不动了,对不起大家。