Spring MVC作为SpringFrameWork的产品,自诞生之日,就受到广泛开发者的关注,如今Spring MVC在Java中的发展可谓是蒸蒸日上,下面详细描述下搭建运行一个Spring MVC的实例。
项目结构如下:
一.在WEB-INF/web.xml,配置Spring MVC转发
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>SpringMVC</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springMVC-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>encodingFilter</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> <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> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
二.在WEB-INF/目录下,创建springMVC-servlet.xml,支持注解,页面路径解析等
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <context:component-scan base-package="com.bijian.study.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views" /> <property name="suffix" value=".jsp" /> </bean> </beans>
三.新建HelloController类,使用注解完成Spring MVC类的调用
package com.bijian.study.controller;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("/greeting")
public ModelAndView greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
//由于浏览器会把中文直接换成ISO-8859-1编码格式,如果用户在地址打入中文,需要进行如下转换处理
try {
String tempName = new String(name.getBytes("ISO-8859-1"),"utf-8");
System.out.println(tempName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("Hello " + name);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userName", name);
return new ModelAndView("/hello", map);
}
}
四.创建/WEB-INF/views/hello.jsp,用来展现数据
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello</title> </head> <body> 你好,${userName} </body> </html>
五.部署运行
部署运行后,在浏览器中输入访问的URL:http://localhost:8080/SpringMVC/greeting?name=zhangshan,运行效果如下:
PS:
http://localhost:8080/SpringMVC/greeting?name=%E7%BE%8E%E5%A5%B3,URIEncoding是UTF-8,在浏览器中会将其转换为中文:http://localhost:8080/SpringMVC/greeting?name=美女,然后这个又会转成ISO-8859-1:http://www.baidu.com/s?wd=%XX%XX%XX%XX
所以,浏览器神奇的将URIEncoding从UTF-8转成ISO-8859-1。
解决的方式也很简单,看看百度:
http://www.baidu.com/s?wd=%B1%E0%BC%AD http://www.baidu.com/s?wd=编辑
URIEncoding就用ISO-8859-1。大多浏览器会把中文直接换成:ISO-8859-1,如果在你的系统里确实需要用户在地址打入中文直接进行查询,建议把URIEncoding改成ISO-8859-1就可以了。但这样以后会造成其它用UTF-8编码的地方就要单独转码了,反而不方便(JSON数据的提交)。所以最好不要让用户在地址栏打入中文进行操作。