1. Spring 项目结构:
2. 基础配置文件:
(1) web.xml
<?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" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(2) spring-mvc-servlet.xml (该xml的命名同web.xml的dispatchServlet的servlet-name, 当然如果想要不同,可以在web.xml下配置不同的xml配置文件名称,供DispatcherServlet初始化配置使用)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.cisco.eStore" />
<context:annotation-config />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3. MVC 文档路径:
Controller在com.xx.estore.controller下,Model在com.xx.estore.model, View在spring-mvc-servlet.xml的配置路径下
(1) Controller:
package com.cisco.eStore.controller;
import com.cisco.eStore.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;
@Controller
@RequestMapping("/admin")
public class HelloWorldController {
@RequestMapping(value = "/user", method = RequestMethod.GET)
public ModelAndView user(){
return new ModelAndView("user", "command", new User());
}
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addUser(@ModelAttribute("SpringWeb")User user, ModelMap model){
model.addAttribute("name", user.getName());
model.addAttribute("password", user.getPassword());
model.addAttribute("email", user.getEmail());
return "result";
}
}
(2) Model:
package com.cisco.eStore.model;
public class User {
private String name;
private String password;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
(3) view:
user.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>用户注册页</title>
</head>
<body>
<h1>This is Add User Page</h1>
<form:form method="POST" action="/admin/addUser">
<table>
<tr>
<td>Name</td>
<td><form:input path="name"/></td>
</tr>
<tr>
<td>Password</td>
<td><form:input path="password"/> </td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>结果页</title>
<style>
label{
margin-right:20px;
}
</style>
</head>
<body>
<h1>This is SpringMVC Result Page/h1>
<label>name:</label><span>${name}</span><br/>
<label>password:</label><span>${password}</span><br/>
<label>email:</label><span>${email}</span><br/>
</body>
</html>
4. Demo功能:
(1) 跳转注册用户页:
上述中Controller.user()方法,会跳转到user.jsp,同时新建user对象到jsp中(2) 添加用户:
上述Controller.addUser()方法,会将页面传来的name, password, email信息填入modelMap中,供result.jsp使用
(3) 展示添加用户信息:
上述Controller.addUser()后 return "result"会直接跳转到 result.jsp, 页面中使用${var}方法获取相应参数