先在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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://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下的jsp
return 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=eco
public 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=eco
public String hello6(String name,Model model){
model.addAttribute("username", name);//这个username可以在下面的jsp页面用el表达式写出来
System.out.println(name);
return "hello";
}

java之SpringMVC的controller配置总结的更多相关文章

  1. SpringMVC:Controller配置总结

    西部开源-秦疆老师:SpringMVC系列博客 , 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringMVC:Controlle ...

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

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

  3. 1.springMVC Controller配置方式

    一.手动配置方式 1.web.xml中DispatcherServlet控制器的的配置 SpringMVC也是一种基于请求驱动的WEB框架,并且使用了前端控制器的设计模式.前端控制器就是Dispatc ...

  4. java:Springmvc框架1(基本配置,注解配置,转换器引入)

    1.springmvc01:(基本配置) web.xml: <?xml version="1.0" encoding="UTF-8"?> <w ...

  5. SpringMVC Controller配置方法有哪几种

    第一种 URL对应Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 表示将请求的URL和Bean名字映射--> <bean class="org.s ...

  6. SpringMVC之三:配置Spring MVC Controller

    一.Controller配置方式 第一种 URL对应Bean如果要使用此类配置方式,需要在XML中做如下样式配置 以上配置,访问/hello.do就会寻找ID为/hello.do的Bean,此类方式仅 ...

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

    这篇主要讲解 controller配置的几种方式. 1. URL对应 Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 配置handlerMapping --> & ...

  8. [Java] Spring + SpringMVC + Maven + JUnit 搭建

    示例项目下载: https://github.com/yangyxd/SpringDemo 利用前面 SpringMVC 项目的配置方式,完成初步的项目创建.下面只讲一些不同之处. 传送门: [Jav ...

  9. java web 之 SpringMVC4.x配置

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q  Q:408365330     E-Mail:egojit@qq.com 综述: 有 ...

随机推荐

  1. Java 8新特性探究(五)Base64详解

    BASE64 编码是一种常用的字符编码,在很多地方都会用到.但base64不是安全领域下的加密解密算法.能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数据的正确性,有些网关或系统只能使 ...

  2. iOS中 读取相册,调用系统相机 技术分享

    技术内容:分别读取相册以及调取相机,将图片显示到imageView上 布局: 1.创建imageView 和 button 并为button一个关联pickerImage的事件 <div sty ...

  3. 谈谈Ext JS的组件——布局的使用方法

    概述 在Ext JS中,包含两类布局:组件类布局和容器类布局.由于有些组件是有不同的组件组合而成的,如字段就由标题和输入框构成,他们之间也是存在布局关系的,而这就需要组件类布局来处理组件内自己特有的布 ...

  4. OJ题:句子逆转

    将一个英文语句以单词为单位逆序排放.例如"I am a boy",逆序排放后为"boy a am I"所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含 ...

  5. Android进阶(十四)Android Adapter详解

    Android Adapter详解 Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View.因为layout五花八门,很多数据都不能直接绑定上去, ...

  6. [转]高级SQL注入:混淆和绕过

    ############# [0×00] – 简介[0×01] – 过滤规避(Mysql)[0x01a] – 绕过函数和关键词的过滤[0x01b] – 绕过正则表达式过滤[0×02] – 常见绕过技术 ...

  7. Android增量升级的方法和原理

    总结: 我们使用delta编码算法减少Android应用升级程序的大小.我们通过bsdiff和bspatch工具在android上实现delta编码算法.服务器软件和android应用已经部署.当前, ...

  8. 第一个Polymer应用 - (3)使用数据绑定

    原文链接: Step 3: Using data binding翻译日期: 2014年7月7日翻译人员: 铁锚我们创建的个人信息卡还算漂亮,但对整个应用来说,只有一张卡片看起来有点空荡荡的感觉.在本节 ...

  9. 速度之王 — LZ4压缩算法(二)

    LZ4 (Extremely Fast Compression algorithm) 项目:http://code.google.com/p/lz4/ 作者:Yann Collet 本文作者:zhan ...

  10. LeetCode(68)-Compare Version Numbers

    题目: Compare two version numbers version1 and version2. If version1 > version2 return 1, if versio ...