先在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. 72【leetcode】经典算法- Lowest Common Ancestor of a Binary Search Tree(lct of bst)

    题目描述: 一个二叉搜索树,给定两个节点a,b,求最小的公共祖先 _______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5 例如: 2,8 - ...

  2. 分布式进阶(十三)Docker Container间实现数据共享

    sudo docker run -it -v /usr/lib:/usr/lib/dbdata --name dbcontainer-192.168.1.184 ubuntu:14.04 sudo d ...

  3. 《java入门第一季》之ArrayList集合小案例

    案例一:去除集合里面的重复元素 package cn.itcast_04; import java.util.ArrayList; /* * ArrayList去除集合中字符串的重复值(字符串的内容相 ...

  4. Leetcode_168_Excel Sheet Column Title

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42554641 Given a positive integ ...

  5. cat .git/config查看远端服务器信息(git的配置信息:远端服务器连接信息)

    本地git库中,查找其连接的远端服务器信息: 每个git库都会有一个配置信息文件.git/config. cat .git/config,可以看到信息如下: [core]         reposi ...

  6. 《java入门第一季》之面向对象(面向对象案例详解)

    通过几个小案例理重新回顾一下前面所写的内容,对面向对象的理解更加深刻的目的: 案例一: /* 需求: 定义一个员工类,自己分析出几个成员, 然后给出成员变量,构造方法,getXxx()/setXxx( ...

  7. ITU-T E.800 有关服务质量(QoS)的术语定义

    摘要 ITU-T E.800建议书为服务质量(QoS)的研究和管理提供了一套通用术语.本建议书列出的与QoS相关的技术和非技术术语旨在代表电信市场所有各方(即用户.服务提供商.制造商和监管机构)的利益 ...

  8. C 打印格式小记

    转自:http://blog.csdn.net/fivedoumi/article/details/7077504 d,lx,ld,,lu,这几个都是输出32位的 hd,hx,hu,这几个都是输出16 ...

  9. 网站开发进阶(六)JSP两种声明变量的区别

    JSP两种声明变量的区别 在JSP中用两种声明变量的方法,一种是在<%! %>内,一种是在<% %>内.他们之间有什么区别呢?我们直接看一个JSP文件来理解. 代码如下: &l ...

  10. 安卓TV开发(九) Android模拟事件 遥控器变身成鼠标来操作TV

    本文出处:http://blog.csdn.net/sk719887916/article/details/40348853,作者:skay      阅读此文建议先阅读 安卓Tv开发(二)移动智能电 ...