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 综述: 有 ...
随机推荐
- 尚学堂马士兵struts2 课堂笔记(三)
19-20 简单数据验证 例如 前台jsp 及struts.xml <a href="user/user!add?name=a" >添加用户</a> < ...
- JAVA数组的定义以及使用1
public class HelloWorld { public static void main(String[] args){ // Scanner s = new Scanner(System. ...
- leetcode之 Palindrome Partitioning I&II
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- MSM平台RPM
Software Component Block Diagram RPM(Resource Power Manager)是高通MSM平台另外加的一块芯片,虽然与AP芯片打包在一起,但其是一个独立的AR ...
- DesignModeler GestureRecgin…
DesignModeler : 设计模式 GestureRecginzer:手势识别 作者:韩俊强 原创版权地址:http://blog.sina.com.cn/s/blog_814ecfa9 ...
- 2013 QCon北京演讲:跨终端的WebKit渲染机制
转载请注明原文地址:http://blog.csdn.net/milado_nju 1. 该演讲主要介绍WebKit的渲染机制的内部工作原理和一些新的技术,特别是针对不断出现的多种终端所做的一些努力. ...
- Ubuntu_ROS中应用kinect v2笔记
Ubuntu_ROS中应用kinect v2笔记 个人觉得最重要的资料如下: 1. Microsoft Kinect v2 Driver Released http://www.ros.org/new ...
- 【cocos 2d-x】VS2012+win7+cocos2d-x3.0beta2开发环境配置
本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder 微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.com ...
- C语言生成32位和64位随机数算法
C语言生成32位和64位随机数算法 /** * randstd.h * * Standard definitions and types, Bob Jenkins * * 2015-01-19: re ...
- LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum
1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...