java之SpringMVC的controller配置总结
先在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配置总结的更多相关文章
- SpringMVC:Controller配置总结
西部开源-秦疆老师:SpringMVC系列博客 , 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringMVC:Controlle ...
- java框架之SpringBoot(5)-SpringMVC的自动配置
本篇文章内容详细可参考官方文档第 29 节. SpringMVC介绍 SpringBoot 非常适合 Web 应用程序开发.可以使用嵌入式 Tomcat,Jetty,Undertow 或 Netty ...
- 1.springMVC Controller配置方式
一.手动配置方式 1.web.xml中DispatcherServlet控制器的的配置 SpringMVC也是一种基于请求驱动的WEB框架,并且使用了前端控制器的设计模式.前端控制器就是Dispatc ...
- java:Springmvc框架1(基本配置,注解配置,转换器引入)
1.springmvc01:(基本配置) web.xml: <?xml version="1.0" encoding="UTF-8"?> <w ...
- SpringMVC Controller配置方法有哪几种
第一种 URL对应Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 表示将请求的URL和Bean名字映射--> <bean class="org.s ...
- SpringMVC之三:配置Spring MVC Controller
一.Controller配置方式 第一种 URL对应Bean如果要使用此类配置方式,需要在XML中做如下样式配置 以上配置,访问/hello.do就会寻找ID为/hello.do的Bean,此类方式仅 ...
- java之spring mvc之Controller配置的几种方式
这篇主要讲解 controller配置的几种方式. 1. URL对应 Bean 如果要使用此类配置方式,需要在XML中做如下样式配置 <!-- 配置handlerMapping --> & ...
- [Java] Spring + SpringMVC + Maven + JUnit 搭建
示例项目下载: https://github.com/yangyxd/SpringDemo 利用前面 SpringMVC 项目的配置方式,完成初步的项目创建.下面只讲一些不同之处. 传送门: [Jav ...
- java web 之 SpringMVC4.x配置
合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综述: 有 ...
随机推荐
- General Ledger Useful SQL Scripts
General Ledger Useful SQL Scripts – Oracle Applications 11i Contents GL Set of Books Configuration O ...
- Socket层实现系列 — I/O事件及其处理函数
主要内容:Socket I/O事件的定义.I/O处理函数的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd I/O事件定义 sock中定义了几个I/ ...
- Chipmunk僵尸物理对象的出现和解决(七)
首先判断问题出现在Star的类方法doStickShorterWork中,于是逐步分词注释代码,最后剩下如下代码: +(void)doStickShorterWork:(Stick *)stick{ ...
- 03_Android项目中读写文本文件的代码
编写一下Android界面的项目 使用默认的Android清单文件 <?xml version="1.0" encoding="utf-8"?> & ...
- Linux Shell 命令--rename
重命名文件,经常用到mv命令,批量重命名文件rename是最好的选择,Linux的rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,判断方法:输入man rename 看到第 ...
- AngularJS进阶(三十四)Angular数据更新不及时问题探讨
Angular数据更新不及时问题探讨 前言 在修复控制角标正确变化过程中,发觉前端代码组织层次出现了严重问题.传递和共享数据时自己使用的是rootScope,为此造成了全局变量空间的污染.根据< ...
- 理解WebKit和Chromium: 网页渲染的基本过程
转载请注明原文地址:http://blog.csdn.net/milado_nju ## 概述 前面介绍了一些渲染引擎的功能,包括网络,资源加载,DOM树,RenderObject树等等,但是,给人以 ...
- AngularJS进阶(二)AngularJS路由问题解决
AngularJS路由问题解决 遇到了一个棘手的问题:点击优惠详情时总是跳转到药店详情页面中去.再加一层地址解决了,但是后来发现问题还是来了: Could not resolve 'yhDtlMain ...
- HashMap原理解析
1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1 ...
- OpenCV分通道显示图片,灰度,融合,直方图,彩色直方图
代码有参考跟整合:没有一一列出出处 // split_rgb.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <io ...