基于注解的Spring MVC
1、加入�jar
2、web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3、springMVC.xml配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!-- 组件扫描 -->
<context:component-scan base-package="cn.itcast.springmvc.controller"></context:component-scan>
<!-- 配置内部资源视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4、实体bean
package cn.itcast.springmvc.domain;
public class User {
private String name;
private String address;
private Integer age;
private String tel;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("正在通过setName方法注入name的值:" + name);
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "{name:" + name + ",address:" + address + ",age:" + age
+ ",tel:" + tel + "}";
}
}
5、编写HomeController,代码例如以下:
package cn.itcast.springmvc.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import cn.itcast.springmvc.domain.User;
/**
* @brief IAccountDao.java 学习Spring注解方式
* @attention
* @author 涂作权
* @date 2014-5-18
* @note begin modify null
*/
@Controller //加入�注解
@RequestMapping(value = "/home") // 根路径,有些类似strut2的命名空间
public class HomeController {
/**
* 子路径,表示仅仅支持get提交
* @param req 能够通过传递HttpServletRequest的方式获得參数
* @param name 表示连接的地方有:XXX?name=
* @param u 假设url的?后面參数过多,要想获得參数,能够直接将这个參数写成User
* @param model :定义一个Map对象,能够通过这样的方式将之传递给jsp页面
*
* @attention url地址能够是:http://localhost:8081/SpringMVC_02/home/hello
* ?name=toto&address=haidian&age=24&tel=136XXX
* 获得的參数为:正在运行hello方法 name:toto User: {name:toto,address:haidian,age:24,tel:136XXX}
* @return
*/
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String hello(HttpServletRequest req,
@RequestParam(value = "name")
String name, User u, Map<String, Object> model) {
//String name = req.getParameter("name");
System.out.println("正在运行hello方法 name:" + name);
System.out.println("User: " + u);
//req.setAttribute("msg", "hello " + name);
model.put("msg", "hello " + name);
return "hello";//逻辑名
}
/**
* \brief 定义方法hi
*
* @return
* @attention url的地方通过/home/hi的方式訪问要想訪问的地址
* @author 涂作权
* @date 2014-5-18
* @note begin modify by null
*/
@RequestMapping(value="/hi") //子路径
public String hi(){
System.out.println("正在运行hi方法");
return "hi"; //逻辑名
}
}
6、编写的hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title> 'hello.jsp'</title>
</head>
<body>
${requestScope.msg}
</body>
</html>
基于注解的Spring MVC的更多相关文章
- Spring7:基于注解的Spring MVC(下篇)
Model 上一篇文章<Spring6:基于注解的Spring MVC(上篇)>,讲了Spring MVC环境搭建.@RequestMapping以及参数绑定,这是Spring MVC中最 ...
- Spring:基于注解的Spring MVC
什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
- Spring6:基于注解的Spring MVC(上篇)
什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
- 基于注解的Spring MVC的简单入门——简略版
网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好...惭愧啊...基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了 ...
- 基于注解的 Spring MVC(上)
什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
- 基于注解的spring mvc 中使用 ajax json 的model
在 Spring mvc3中,响应.接受 JSON都十分方便. 使用注解@ResponseBody可以将结果(一个包含字符串和JavaBean的Map),转换成JSON. 使用 @RequestBod ...
- 基于注解的 Spring MVC 简单入门
web.xml 配置: <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> ...
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
1.导入jar watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400 ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
随机推荐
- Linux中的小程序—— 进度条
在说正事之前,首先科普一下在什么是回车什么是换行? 我们通常所说的回车就是从一行的末尾跳到另一行的开头,但事实上这却是由回车和换行两个动作所完成的,也就是键盘上<enter>所完成的工作. ...
- 华硕笔记本怎么设置u盘启动(两种方法)
华硕笔记本怎么设置u盘启动(两种方法) 华硕笔记本怎么设置u盘启动.我想用U盘安装系统但是 我不知道如何设置U盘启动,那么该如何设置呢?下面和大家分享一下我的经验,希望能够帮到大家.如果你的系统是预装 ...
- C#6.0语法糖
using System; using static System.Math;//using static,仅仅引入类中的静态方法 namespace _6._0Syntax { class Prog ...
- C#简单注册表操作实例
1.简介操作 //设置注册值 private void Button_Click(object sender, RoutedEventArgs e) { //路径及间隔符号要正确 //1.如果指定路径 ...
- plsql编程中游标的使用
游标(Cursor):用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作. oracle中显示使用游标一般要包含以下5个步骤: 声明一些变量以便存储从游 ...
- FANTASY:In which way do you think the world will end?
In which way do you think the world will end? The moment you are reading my essay, you are somehow c ...
- windows搭建redis记录
windows安装redis:http://www.cnblogs.com/linjiqin/archive/2013/05/27/3101694.html 30个常用的redis命令:http:// ...
- 关于把A表中的数据复制到B表中。
最近公司需要把sql中的数据给整理出来,这就牵涉到数据转移问题. 我平时是很少接触sql这一块的.所以碰到这个问题甚是伤脑筋. 不过还好,这问题并不像我想象中的那么的困难. 以前做过把数据插入到临时表 ...
- Nokia N9开启开发者模式
最近淘宝买个二手Nokia N9,纯粹是好奇meego系统. 到手了开始折腾,官方源早关闭了,导致无法开启开发者模式,没有权限很不方便.翻了翻dospy论坛的帖子,发现了n9repomirror_0. ...
- Anisotropic gauss filter
最近一直在做版面分析,其中文本行检测方面,许多文章涉及到了Anigauss也就是各向异性高斯滤波. 顾名思义,简单的理解就是参数不同的二维高斯滤波. 在文章Fast Anisotropic Gauss ...