一个简单的MVC框架,基本要熟悉以下步骤
1:定义一个Controller类
2:配置如何配置
3:如何定义url地址 进入方法
4:如何将查询的数据放入作用域(拿到request,response对象)
5:如何返回页面,转发/重定向
springmvc的配置:
1: 引入jar包
commons-beanutils-1.8.0.jar
commons-fileupload-1.2.1.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
commons-pool-1.2.jar
spring-aop-4.1.5.RELEASE.jar
spring-aspects-4.1.5.RELEASE.jar
spring-beans-4.1.5.RELEASE.jar
spring-context-4.1.5.RELEASE.jar
spring-context-support-4.1.5.RELEASE.jar
spring-core-4.1.5.RELEASE.jar
spring-expression-4.1.5.RELEASE.jar
spring-web-4.1.5.RELEASE.jar
spring-webmvc-4.1.5.RELEASE.jar
spring-webmvc-portlet-4.1.5.RELEASE.jar
2:在web.xml定义Springmvc的核心类Servlet类
springmvc的配置和applicationContext.xml是一种父子关系
在web-inf下建立一个叫:"servlet-name"-servlet.xml文件 zpupload-servlet.xml
如果想改变名称:在servlet核心类中加入:
namespace
fylupload
web.xml中所有代码如下:
zpupload
index.jsp
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
zpupload
org.springframework.web.servlet.DispatcherServlet
namespace
fylupload
zpupload
*.html
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*
3:配置fylupload.xml
4:定义一个类IndexController测试
package com.zp.upload;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/upload")
public class IndexController {
@RequestMapping("/test")
public String test() {
System.out.println("进来了嘛??");
return null;
}
// http://localhost:8080/zpupload/upload/index1.html
@RequestMapping("/index1")
public String index1(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
HashMap map = new HashMap<>();
map.put("name", "MM-index1");
map.put("age", "17");
// 把map放入作用域,jsp用EL表达式取值
request.setAttribute("map", map);
// 返回page,路径为视图渲染配种中的
// name="prefix"+@RequestMapping("/upload")+@RequestMapping("/index1")+name="suffix"
// 当我们访问 http://localhost:8080/zpupload/upload/index1.html 他实际找的jsp文件为
// /WEB-INF/pages/upload/index1.jsp
return "index1";
}
@RequestMapping("index2")
public ModelAndView index2() {
ModelAndView modelAndView = new ModelAndView();
HashMap map = new HashMap<>();
map.put("name", "MM-index2-ModelAndView");
map.put("age", "18");
modelAndView.addObject("map", map);// request.setAttribute("map", map);
modelAndView.setViewName("upload/index2");// return "index1";
return modelAndView;
}
// 带参数的get访问
// http://localhost:8080/zpupload/upload/index3.html?username=你好
@RequestMapping("index3")
public String index3(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String username = request.getParameter("username");
System.out.println("username========>" + username);
HashMap map = new HashMap<>();
map.put("name", "MM-index3-Parameter");
map.put("age", "18");
request.setAttribute("map", map);
return "index3";
}
// 重定向:http://localhost:8080/zpupload/upload/index4.html?username=你好
@RequestMapping("index4")
public String index4(HttpServletRequest request) {
String username = request.getParameter("username");
System.out.println("username========>" + username);
HashMap map = new HashMap<>();
map.put("name", "MM-index4-redirect");
map.put("age", "18");
request.setAttribute("map", map);
//return "redirect:success.html?username=mm";
//return "redirect:/success.jsp";
return "forward:/success.jsp";
/**
* return "redirect:/success.jsp";时
* 会跳转 http://localhost:8080/zpupload/success.jsp
* 访问WebContent下的success.jsp文件
* return "redirect:success.jsp"; ---> 不加/
* 是基于命名空间@RequestMapping("/upload"),会跳转/zpupload/upload/success.jsp
* 重定向(不走视图解析,不会拼接路径)一定是webroot下面的页面或者是requestMapping路径,
* WEB-INF下的文件不允许直接访问,需要借助中间跳转
* return "redirect:/success.html"; 时
* 会跳转 http://localhost:8080/zpupload/upload/success.html
* 被@RequestMapping("/success")拦截 访问return "common/success";
* 访问 /WEB-INF/pages/common/success.jsp 文件
*
* return "forward:/success.jsp";
* 转发也会跳过视图解析,不拼接路径,访问WebContent下的success.jsp文件
*/
}
@RequestMapping("/success")
public String success() {
return "common/success";
}
}
代码详见
http://download.csdn.net/detail/u013820054/9732369