1、web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc1</display-name> <filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>springmvc</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>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> </web-app>

2、springmvc.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 把Controller交给spring管理 -->
<context:component-scan base-package="com.xiaostudy"/> <!-- 配置注解处理器映射器 功能:寻找执行类Controller -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!-- 配置注解处理器适配器 功能:调用controller方法,执行controller -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 配置sprigmvc视图解析器:解析逻辑试图
后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/index.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

3、domain类

 package com.xiaostudy.domain;

 public class User {

     private int id;
private String username;
private String password;
private int age; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";
} }

4、封装domain类

 package com.xiaostudy.controller;

 import com.xiaostudy.domain.User;

 public class CustomUser {

     private User user;

     public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} @Override
public String toString() {
return "CustomUser [user=" + user + "]";
} }

5、注解类

 package com.xiaostudy.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.xiaostudy.domain.User; @Controller//<bean class="com.xiaostudy.controller.MyController"/>
@RequestMapping(value="/myController")//访问该类的方法时,前面多这样一个路径
public class MyController { // @RequestMapping("hello")//http://localhost:8080/demo2/hello.do
// @RequestMapping("/hello")//http://localhost:8080/demo2/hello.do
// @RequestMapping(value="/hello.do")//http://localhost:8080/demo2/hello.do
// @RequestMapping(value="/hello.do",method=RequestMethod.GET)//http://localhost:8080/demo2/hello.do
// @RequestMapping(value="/hello.do",method= {RequestMethod.GET,RequestMethod.POST})//http://localhost:8080/demo2/hello.do
public String print() {
return "index";
} @RequestMapping("hi")//http://localhost:8080/demo2/myController/hi.do
public String hello() {
return "index";
} @RequestMapping("requestint")//http://localhost:8080/demo2/myController/requestint.do
public String requestint(int id) {
System.out.println(id);
return "index";
} @RequestMapping("requestint2")//http://localhost:8080/demo2/myController/requestint2.do
public String requestint2(int id, int i) {
System.out.println(id + " " + i);
return "index";
} @RequestMapping("requestint3")//http://localhost:8080/demo2/myController/requestint3.do
public String requestint3(User user) {
System.out.println(user);
return "index";
} @RequestMapping("requestint4")//http://localhost:8080/demo2/myController/requestint4.do
public String requestint4(CustomUser customUser) {
System.out.println(customUser);
return "index";
} @RequestMapping("xiaostudy")//http://localhost:8080/demo2/myController/hi.do
public String add() {
return "xiaostudy";
} }

6、填写表单数据的xiaostudy.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>springMVC_demo</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/myController/requestint.do">
<fieldset>
<legend>单独一个参数</legend>
<input type="text" name="id" id="id"/>
<input type="submit" value="提交">
</fieldset>
</form> <form action="${pageContext.request.contextPath }/myController/requestint2.do">
<fieldset>
<legend>两个参数</legend>
<input type="text" name="i" id="i"/>
<input type="text" name="id" id="id"/>
<input type="submit" value="提交">
</fieldset>
</form> <form action="${pageContext.request.contextPath }/myController/requestint3.do">
<fieldset>
<legend>参数为一个对象</legend>
<input type="text" name="id" id="id"/>
<input type="text" name="username" id="username"/>
<input type="password" name="password" id="password"/>
<input type="text" name="age" id="age"/>
<input type="submit" value="提交">
</fieldset>
</form> <form action="${pageContext.request.contextPath }/myController/requestint4.do">
<fieldset>
<legend>参数为一个封装对象</legend>
<input type="text" name="user.id" id="id"/>
<input type="text" name="user.username" id="username"/>
<input type="password" name="user.password" id="password"/>
<input type="text" name="user.age" id="age"/>
<input type="submit" value="提交">
</fieldset>
</form>
</body>
</html>

7、跳转的index.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>springMVC_demo</title>
</head>
<body>
xiaostudy
</body>
</html>

项目文件结构


springMVC注解的参数传递的更多相关文章

  1. springmvc注解和参数传递

    一.SpringMVC注解入门 1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- mvc的注解驱动 --> <mvc:annotation ...

  2. springmvc入门基础之注解和参数传递

    一.SpringMVC注解入门 1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- mvc的注解驱动 --> <mvc:annotation ...

  3. springMVC 注解版

    http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...

  4. springMVC注解初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  5. SpringMVC注解开发初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  6. SpringMVC注解汇总(二)-请求映射规则

    接上一节SpringMVC注解汇总-定义 讲到Httpy请求信息 URL路径映射 1)普通URL路径映射 @RequestMapping(value={"/test1", &quo ...

  7. springMVC注解启用及优化

    使用注解的原因 最方便的还是启用注解 注解方便,而且项目中很流行. 配置文件尽量减少,主要使用注解方式. Springmvc的注解是在2.5版本后有了注解,如何开启注解配置文件 Web.xml文件中不 ...

  8. 6.SpringMVC注解启用

    SpringMVC注解可以帮助我们快速地注入 属性和参数 提高开发效率. 由于 有相当一部分人讨厌xml配置方式 注解可以覆盖 xml则不能 使用注解比xml规范化,因为很多注解都是java的规范的范 ...

  9. springMVC(注解版笔记)

    springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...

随机推荐

  1. 常用移动web开发框架--转载

      阅读目录 1.1 jQuery mobile flat-ui 主题 1.2jQuery mobile Bootstrap 主题 4.1 GMU 4.2 Clouda+ 4.3 efe 5.1 Sp ...

  2. openresty环境搭建问题记录

    第一次在mac安装遇到如下问题: 截图: 具体code如下: Sonofelice:bch-flowrouter baidu$ brew install openresty/brew/openrest ...

  3. golang处理 json中非法字符

    原文: Hi there, I just discovered Go and decided to port a little program to Go. The program reads JSO ...

  4. DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_memory=False.

    DtypeWarning: Columns (1,5,7,16,......) have mixed types. Specify dtype option on import or set low_ ...

  5. MySQL索引优化案例浅析

    MySQL是关系型数据库的一种,查询功能强,数据一致性高,数据安全性高,支持二级索引.但是性能比起非关系型数据库稍弱,特别是百万级以上的数据,很容易出现查询慢的现象.这时候要分析慢的原因,一般情况下是 ...

  6. php内存溢出,出现Allowed memory size of 8388608 bytes exhausted错误的解决办法

    是因为php页面消耗的最大内存默认是为128M (在PHP的ini件里可以看到) ,如果文件太大或图片太大在读取的时候会发生上述错误. 解决办法: 1.修改 php.ini 将memory_limit ...

  7. 脚本其实很简单-windows配置核查程序(2)

    bat脚本是什么? 首先讲讲什么是命令行,在windows操作系统中,点击左下角的win图标,直接输入cmd搜索,左键点击进入命令行模式(或按键盘上的win键+r直接调出来命令行窗口). 在windo ...

  8. 随机深林和GBDT

    随机森林(Random Forest): 随机森林是一个最近比较火的算法,它有很多的优点: 在数据集上表现良好 在当前的很多数据集上,相对其他算法有着很大的优势 它能够处理很高维度(feature很多 ...

  9. 【npm】使用淘宝提供的镜像来加速npm

    国外的npm用着非常不稳定,时常一直就卡在安装的进度条上 淘宝提供了一个国内镜像,保障了安装网络环境的稳定,和源地址10分钟同步一次,没有被收录的包会自动切换到npm官方下载,并添加进镜像库. 地址: ...

  10. javaWeb 使用线程池+队列解决"订单并发"问题

    解决方式:使用线程池+队列 项目基于Spring,如果不用spring需要自己把 ThreadPoolManager.java 改成单例模式 1.写一个Controller(Spring mvc) / ...