本文主要描述spring3 mvc 的环境搭载,适合初学者参考!算是spring3 mvc 的hello word吧!
跟着步骤做吧:
1、首先配置web.xml,每行代码都已经注明:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 添加spring MVC支持 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定初始化参数 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 指明了配置文件的文件名,不使用默认配置文件名,而使用springmvc-config.xml配置文件在程序的根目录下 ,多个用逗号隔开-->
<!-- 这里如果不指定,使用默认值:/WEB-INF/<servlet-name>-servlet.xml -->
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 设置启动顺序,让这个Servlet随Servlet容器一起启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 映射上面的servlet -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 添加过滤器,为每个请求设置字符编码为utf-8 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、配置springMVC-config.xml,该文件在程序的根目录下,这是一个最基本的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 默认的注解映射支持 -->
<mvc:annotation-driven/>
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.test.web.controller" />
<!-- 视图解释类 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/page/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3、编写Controller
package com.test.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.test.bean.User;
import com.test.web.validators.UserValidator;
@Controller
// 改注解可以省略,可以表示访问的包名吧
@RequestMapping("/user")
public class UserController{
// 初始化绑定了一个自定义验证器,后续做介绍,忽略。。。
@InitBinder
protected void initBinder(WebDataBinder binder){
binder.setValidator(new UserValidator());
}
// 请求映射,value和表单的action一致,mothod表明表单的提交方式 @Validated指定了user对象需要验证
@RequestMapping(value="hello",method=RequestMethod.POST)
public ModelAndView helloWorld(@Validated User user,BindingResult result,Model model) {
if (result.hasErrors()) {
// 回填表单信息,不至于用户请求失败后,数据清空
model.addAttribute("user", user);
// 请求结果跳转的视图名称
return new ModelAndView("hello");
}
return new ModelAndView("sucess", "user", user);
}
// 注意@ModelAttribute类似struts2中的模型驱动接口(将请求的表单直接封装成User对象了)
@RequestMapping(value="index")
public ModelAndView index(@ModelAttribute User user){
return new ModelAndView("hello").addObject(user);
}
}
到此为止,spring MVC基本环境配置结束,测试form就没必要说明了,动手写个jsp测试下吧!