前言
领导把我调到另一个组帮忙,组里的项目是用Spring MVC框架写的,学习Spring MVC就成了我的头等大事,接下来我会将学习过程记录下来,方便自己复习,也希望对同我一样正在学习或准备学习的同学有帮助。好了,开始吧。
环境
win64+eclipse+tomcat 6+jdk1.7
项目目录结构
填写用户信息,提交后显示提交的内容的内容。目录结构如下,包括需要添加的jar包:
部署描述符文件和Spring MVC配置文件
部署描述符文件(web.xml)
添加如下配置:
<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>
<!-- 匹配所有的请求到DispatcherServlet -->
<url-pattern>/</url-pattern>
</servlet-mapping>
这里告诉了我们Servlet/JSP容器,我们将使用Spring MVC的Dispatcher Servlet,并通过配置url-pattern元素值为"/",将所有的URL映射到该servlet。
Spring MVC配置文件(springmvc-servlet.xml)
添加如下配置:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
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.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-3.1.xsd">
<bean name="/user_input.action"
class="com.zpq.springmvc.controller.InputUserController">
</bean>
<bean name="/user_save.action"
class="com.zpq.springmvc.controller.SaveUserController">
</bean>
</beans>
这里声明了InputUserController和SaveUserController两个控制器类,并分别映射到/user_input.action和/user_save.action。
Bean
User.java
设置bean属性和方法
package com.zpq.springmvc.domain;
import java.io.Serializable;
public class User implements Serializable
{
private static final long serialVersionUID = 1234567L;
private String name;
private String description;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
Form
UserForm.java
表单数据封装在该对象中
package com.zpq.springmvc.form;
import java.io.Serializable;
public class UserForm implements Serializable
{
private static final long serialVersionUID = 1234567L;
private String name;
private String description;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
Controller
InputUserController.java
实现Controller接口
package com.zpq.springmvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class InputUserController implements Controller
{
private static final Log logger = LogFactory.getLog(InputUserController.class);
/*
* 返回一个ModelAndView,包含一个视图,且没有模型
*/
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception
{
// TODO Auto-generated method stub
logger.info("InputProductController called");
//转发到下一个页面
return new ModelAndView("/WEB-INF/jsp/UserForm.jsp");
}
}
SaveUserController.java
实现Controller接口
package com.zpq.springmvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.zpq.springmvc.domain.User;
import com.zpq.springmvc.form.UserForm;
public class SaveUserController implements Controller
{
private static final Log logger = LogFactory.getLog(SaveUserController.class);
@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse reponse) throws Exception
{
// TODO Auto-generated method stub
logger.info("SaveProductController called");
//populate action properties
//以后的篇章中将会学习并分享如何在Spring MVC中省去UserForm对象
UserForm userform = new UserForm();
userform.setName(request.getParameter("name"));
userform.setDescription(request.getParameter("description"));
/*
* create model
* 根据ProductForm对象创建Product对象
*/
User user = new User();
user.setName(userform.getName());
user.setDescription(userform.getDescription());
/*
* insert code to save Product
* 返回包括视图路径、模型名称、模型(对象)
* 该模型将提供给目标视图,用于界面显示
*/
return new ModelAndView("/WEB-INF/jsp/UserDetails.jsp", "user", user);
}
}
View
UserForm.jsp
供提交表单数据
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add User Form</title>
</head>
<body>
<form action="user_save.action" method="post">
<fieldset>
<legend>Add a user</legend>
<label for="name">User Name: </label>
<input type="text" id="name" name="name" value="" tabindex="1">
<label for="description">Description: </label>
<input type="text" id="description" name="description" tabindex="2">
<label for="dummy"> </label>
<input id="reset" type="reset" tabindex="3">
<input id="submit" type="submit" tabindex="4" value="Add User">
</fieldset>
</form>
</body>
</html>
UserDetails.jsp
显示提交的数据
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Prodduct</title>
</head>
<body>
<h4>The user has been saved.</h4>
<p>
<h5>Details:</h5>
Product Name: ${user.name}<br/>
Description: ${user.description}<br/>
</p>
</body>
</html>
测试应用
意外发现乱码
原因可能在UserDetails.jsp中:
方法一
将“ISO-8859-1”改成了“UTF-8”
乱码还是出现。
方法二
进入SaveUserController.java,修改代码,将从request中得到的数据的字符集强行转换成UTF-8:
request.setCharacterEncoding("utf-8");
修改后如下所示:
//populate action properties
UserForm userform = new UserForm();
//将从request中得到的数据的字符集强行转换成UTF-8
request.setCharacterEncoding("utf-8");
userform.setName(request.getParameter("name"));
userform.setDescription(request.getParameter("description"));
问题解决,如图所示:
最后附上案例:http://pan.baidu.com/s/1eRXb0Fw