先在springmvc-servlet.xml文件作如下配置(注解开发controller)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    <!-- don't handle the static resource --><mvc:default-servlet-handler /><!-- if you use annotation you must configure following setting --><mvc:annotation-driven /><!-- scan the package and the sub package --><context:component-scan base-package="com.eco.controllor"/><!-- configure the InternalResourceViewResolver视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"><!-- 前缀 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 后缀 --><property name="suffix" value=".jsp" /></bean>
</beans>

然后来看看,在有无视图解析器的情况下,转发和重定向的实现

@Controller//定义这是一个控制器
public class CController {@RequestMapping("/hello1")//浏览器访问路径public ModelAndView hello1() {ModelAndView mv = new ModelAndView();mv.addObject("msg", "world");//msg可以用el表达式在下面的页面写出来mv.setViewName("01.jsp");//没有视图解析器到根目录的jsp//mv.setViewName("hello");//有视图解析器到web-inf下的jspreturn mv;
    }@RequestMapping("/hello2")public String hello2() {return "hello";//转发:使用视图解析器就转到web-inf下的jsp//return "redirect:hello1";//重定向:视图解析器到hello1,然后hello1转到web-inf下的hi.jsp//return "hello.jsp";//转发:不使用视图解析器转到根目录下的jsp//return "forward:01.jsp";//转发:不使用视图解析器根目录下的jsp//return "redirect:01.jsp";//重定向:不使用视图解析器根目录下的jsp
    }@RequestMapping("/hello3")public void hello3(HttpServletRequest req,HttpServletResponse resp) throws Exception{        req.setAttribute("a", "就是我");//a可以用el表达式在下面的页面写出来        req.getRequestDispatcher("01.jsp").forward(req, resp);//请求转发到根目录下的jsp--不需要视图解析器         //resp.sendRedirect("01.jsp");//请求重定向到根目录下的jsp--不需要视图解析器  } }

看完页面跳转,下面再来看看数据的处理(表单)

    @RequestMapping("/hello4")//http://localhost:8080/webmvc/hello4?name=ecopublic String hello4(String name){System.out.println(name);return "hello";}@RequestMapping("/hello5")//http://localhost:8080/webmvc/hello5?name=eco&pwd=112313//User类的成员变量和域名称一样public String hello5(User user){System.out.println(user);return "hello";}@RequestMapping("/hello6")//http://localhost:8080/webmvc/hello6?name=ecopublic String hello6(String name,Model model){model.addAttribute("username", name);//这个username可以在下面的jsp页面用el表达式写出来
        System.out.println(name);return "hello";}

转载于:https://www.cnblogs.com/eco-just/p/7882016.html

java之SpringMVC的controller配置总结相关推荐

  1. java框架之SpringBoot(5)-SpringMVC的自动配置

    本篇文章内容详细可参考官方文档第 29 节. SpringMVC介绍 SpringBoot 非常适合 Web 应用程序开发.可以使用嵌入式 Tomcat,Jetty,Undertow 或 Netty ...

  2. java mvc controller_java之spring mvc之Controller配置的几种方式

    这篇主要讲解 controller配置的几种方式. 1. URL对应 Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 2. 为 URL 分配 Bean 使用一个统一配置集合,对各个 U ...

  3. SpringMVC之Controller查找(Spring4.0.3/Spring5.0.4源码进化对比)

    0 摘要 本文从源码层面简单讲解SpringMVC的处理器映射环节,也就是查找Controller详细过程 1 SpringMVC请求流程 Controller查找在上图中对应的步骤1至2的过程 Sp ...

  4. Java EE——SpringMVC框架学习

    文章目录 一.SpringMVC的基本概念: 1.三层架构和MVC: 2.SpringMVC的概述: 3.SpringMVC在三层架构的位置: 二.SpringMVC 的入门: 三.入门案例的执行过程 ...

  5. SpringMVC从Controller跳转到另一个Controller

    [PK亲测] 能正常跳转的写法如下: return "forward:aaaa/bbbb.do"; return "redirect:aaaa/bbbb.do" ...

  6. SpringMVC之Controller和参数绑定

    在上一篇Spring+SpringMVC+Mybatis整合中说到了SSM的整合,并且在其中添加了一个简单的查询功能,目的只是将整个整合的流程进行一个梳理,下面在上一篇中工程的基础上再说一些关于Spr ...

  7. Java框架 SpringMVC介绍及入门案例

    1.SpringMVC简介 1.1.什么是MVC MVC是一种软件架构的思想,将软件按照模型.视图.控制器来划分 M:Model ,模型层,指工程中的 JavaBean ,作用是处理数据 JavaBe ...

  8. 【狂神说Java】SpringMVC最新教程IDEA版通俗易懂

    目录 狂神视频地址 1.什么是MVC 1.1.Model1时代 1.2.Model2时代 1.3.回顾Servlet 2.什么是SpringMVC 2.1.概述 2.2.中心控制器 2.3.第一个MV ...

  9. SpringMVC最简单配置应用

    原文地址:https://www.cnblogs.com/wuxinyiwu/p/7552013.html 一.项目配置 1.建立java web项目 2.导入相关jar包 3.配置web.xml文件 ...

  10. Marco's Java【SpringMVC入门(五) 之 SpringMVC的拦截器的使用】

    前言 我们知道在web开发中,一般有三大板块:Servlet(服务连接器) .Listener(监听器) 和Filter(过滤器),而今天我们要学习的拦截器可以算是一个精致的过滤器"法宝&q ...

最新文章

  1. windows IIS权限经典设置教程
  2. python实现中文图片文字识别--OCR about chinese text--tesseract
  3. 你的微信,到底「连接」多少人?
  4. SpringMVC路径匹配规则AntPathMatcher(转)
  5. 使用Spring WebFlux进行操作
  6. 【渝粤教育】国家开放大学2018年秋季 2508T学前儿童语言教育 参考试题
  7. hadoop 配置文件
  8. java应用程序做授权_java-为我的Web应用程序编写授权过滤器(JSF ...
  9. 解决启动nginx时报80端口被占用的问题
  10. Java之品优购课程讲义_day09(2)
  11. ba无标度网络python_python绘制BA无标度网络
  12. 阿里反腐需要一次“遵义会议”
  13. python 实现维基百科六度分隔原理
  14. 将图片(HDC)打印出来
  15. C#实现多人语音聊天
  16. ES2015 class
  17. python工程师怎么考-【一个合格的Python工程师需要达到怎样的编程水平】
  18. HDU 1493 QQpet exploratory park(概率DP)
  19. 如何用织梦在本地搭建网站?
  20. adb模拟按键home_【安卓试玩】电脑端adb操作手机或模拟器设备,可自行实现中控功能。 _ 一只鱼插件 - 按键精灵论坛...

热门文章

  1. 时间序列分析工具箱——tibbletime
  2. bzoj 5084: hashit
  3. 轻松解决U盘拷贝文件时提示文件过大问题
  4. 随机数------选双色球
  5. 24-语言入门-24-cigarettes
  6. 现实世界的Windows Azure: 采访LexisNexis的Marc Slovak 和 Manish Bhargava
  7. 给控件做数字签名之二:生成证书文件
  8. mysqldump备份所有数据库,恢复单个库的场景预演
  9. 陕西大数据产业建设总投资达500亿元
  10. 一周总结汇总_2016-09-25