先在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. 【一天一道LeetCode】#95. Unique Binary Search Trees II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 解决 RtlCreateActivationContext() failed 0xc000000d

    gtest 示例的Debug版启动报错: Debug输出如下: 'sample1_unittest.exe': Loaded 'D:\LibSrc\gtest_1.7.0_build\Debug\sa ...

  3. Libgdx 1.6.0发布,跨平台游戏开发框架

    [1.6.0] -英文原文:http://www.badlogicgames.com/wordpress/?p=3682 -API更改:GlyphLayout xAdvances现在有了额外的开始入口 ...

  4. Touch Handling in Cocos2D 3.x(六)

    使英雄变成可触碰的对象 这是另一个非常有用的特性.很多用户需要捡起已经存在的英雄然后满屏幕移动它们.让我们按以下步骤实现该功能: 如果用户触摸屏幕空白位置,一个新的英雄将被创建 如果用户触摸一个已经存 ...

  5. Linux/Android多点触摸协议

    链接点击打开链接 关于Linux多点触摸协议大家可以参考kernel中的文档:https://www.kernel.org/doc/Documentation/input/multi-touch-pr ...

  6. 深入 JAVA里面关于byte数组和String之间的转换问题

    把byte转化成string,必须经过编码.  例如下面一个例子:  importjava.io.UnsupportedEncodingException; publicclass test{ pub ...

  7. python异常处理和断言

    http://blog.csdn.net/pipisorry/article/details/21841883 关于异常处理有必要么的讨论 最重要的问题是你在开发过程中隐藏了bug,如果当时你没加这个 ...

  8. STM32学习笔记(一)时钟和定时器

    由于近期在准备海洋航行器比赛,正好趁此机会学习一下ARM,看到周围很多同学都在使用32,所以我也买了一块STM32F103ZET6,准备好好地学习一下. STM32的时钟系统相当的复杂,包含了5个时钟 ...

  9. HBASE表设计

    1. 表的设计 1.1 Pre-Creating Regions 默认情况下,在创建HBase表的时候会自动创建一个region分区,当导入数据的时候,所有的HBase客户端都向这一个region写数 ...

  10. 对FMDB的封装JRDB

    在自己开发中,每次用到数据库都会纠结是使用CoreData还是FMDB.CoreData虽然Api简单,但是调用栈非常复杂,要初始化一个Context需要至少20行代码.显然,对于这种这么恶心的情况, ...