1、web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>springmvc1</display-name>
  4.  
  5. <filter>
  6. <filter-name>characterEncoding</filter-name>
  7. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  8. <init-param>
  9. <param-name>encoding</param-name>
  10. <param-value>UTF-8</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>characterEncoding</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>
  17.  
  18. <servlet>
  19. <servlet-name>springmvc</servlet-name>
  20. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  21. <init-param>
  22. <param-name>contextConfigLocation</param-name>
  23. <param-value>classpath:springmvc.xml</param-value>
  24. </init-param>
  25. </servlet>
  26. <servlet-mapping>
  27. <servlet-name>springmvc</servlet-name>
  28. <url-pattern>*.do</url-pattern>
  29. </servlet-mapping>
  30.  
  31. <welcome-file-list>
  32. <welcome-file>index.html</welcome-file>
  33. <welcome-file>index.htm</welcome-file>
  34. <welcome-file>index.jsp</welcome-file>
  35. <welcome-file>default.html</welcome-file>
  36. <welcome-file>default.htm</welcome-file>
  37. <welcome-file>default.jsp</welcome-file>
  38. </welcome-file-list>
  39.  
  40. </web-app>

2、springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context-3.2.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  16. http://www.springframework.org/schema/tx
  17. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
  18.  
  19. <!-- 把Controller交给spring管理 -->
  20. <context:component-scan base-package="com.xiaostudy"/>
  21.  
  22. <!-- 配置注解处理器映射器 功能:寻找执行类Controller -->
  23. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
  24.  
  25. <!-- 配置注解处理器适配器 功能:调用controller方法,执行controller -->
  26. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
  27.  
  28. <!-- 配置sprigmvc视图解析器:解析逻辑试图
  29. 后台返回逻辑试图:index
  30. 视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/index.jsp -->
  31. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  32. <property name="prefix" value="/WEB-INF/"/>
  33. <property name="suffix" value=".jsp"/>
  34. </bean>
  35. </beans>

3、domain类

  1. package com.xiaostudy.domain;
  2.  
  3. public class User {
  4.  
  5. private int id;
  6. private String username;
  7. private String password;
  8. private int age;
  9.  
  10. public int getId() {
  11. return id;
  12. }
  13.  
  14. public void setId(int id) {
  15. this.id = id;
  16. }
  17.  
  18. public String getUsername() {
  19. return username;
  20. }
  21.  
  22. public void setUsername(String username) {
  23. this.username = username;
  24. }
  25.  
  26. public String getPassword() {
  27. return password;
  28. }
  29.  
  30. public void setPassword(String password) {
  31. this.password = password;
  32. }
  33.  
  34. public int getAge() {
  35. return age;
  36. }
  37.  
  38. public void setAge(int age) {
  39. this.age = age;
  40. }
  41.  
  42. @Override
  43. public String toString() {
  44. return "User [id=" + id + ", username=" + username + ", password=" + password + ", age=" + age + "]";
  45. }
  46.  
  47. }

4、封装domain类

  1. package com.xiaostudy.controller;
  2.  
  3. import com.xiaostudy.domain.User;
  4.  
  5. public class CustomUser {
  6.  
  7. private User user;
  8.  
  9. public User getUser() {
  10. return user;
  11. }
  12.  
  13. public void setUser(User user) {
  14. this.user = user;
  15. }
  16.  
  17. @Override
  18. public String toString() {
  19. return "CustomUser [user=" + user + "]";
  20. }
  21.  
  22. }

5、注解类

  1. package com.xiaostudy.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6.  
  7. import com.xiaostudy.domain.User;
  8.  
  9. @Controller//<bean class="com.xiaostudy.controller.MyController"/>
  10. @RequestMapping(value="/myController")//访问该类的方法时,前面多这样一个路径
  11. public class MyController {
  12.  
  13. // @RequestMapping("hello")//http://localhost:8080/demo2/hello.do
  14. // @RequestMapping("/hello")//http://localhost:8080/demo2/hello.do
  15. // @RequestMapping(value="/hello.do")//http://localhost:8080/demo2/hello.do
  16. // @RequestMapping(value="/hello.do",method=RequestMethod.GET)//http://localhost:8080/demo2/hello.do
  17. // @RequestMapping(value="/hello.do",method= {RequestMethod.GET,RequestMethod.POST})//http://localhost:8080/demo2/hello.do
  18. public String print() {
  19. return "index";
  20. }
  21.  
  22. @RequestMapping("hi")//http://localhost:8080/demo2/myController/hi.do
  23. public String hello() {
  24. return "index";
  25. }
  26.  
  27. @RequestMapping("requestint")//http://localhost:8080/demo2/myController/requestint.do
  28. public String requestint(int id) {
  29. System.out.println(id);
  30. return "index";
  31. }
  32.  
  33. @RequestMapping("requestint2")//http://localhost:8080/demo2/myController/requestint2.do
  34. public String requestint2(int id, int i) {
  35. System.out.println(id + " " + i);
  36. return "index";
  37. }
  38.  
  39. @RequestMapping("requestint3")//http://localhost:8080/demo2/myController/requestint3.do
  40. public String requestint3(User user) {
  41. System.out.println(user);
  42. return "index";
  43. }
  44.  
  45. @RequestMapping("requestint4")//http://localhost:8080/demo2/myController/requestint4.do
  46. public String requestint4(CustomUser customUser) {
  47. System.out.println(customUser);
  48. return "index";
  49. }
  50.  
  51. @RequestMapping("xiaostudy")//http://localhost:8080/demo2/myController/hi.do
  52. public String add() {
  53. return "xiaostudy";
  54. }
  55.  
  56. }

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

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3. "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>springMVC_demo</title>
  8. </head>
  9. <body>
  10. <form action="${pageContext.request.contextPath }/myController/requestint.do">
  11. <fieldset>
  12. <legend>单独一个参数</legend>
  13. <input type="text" name="id" id="id"/>
  14. <input type="submit" value="提交">
  15. </fieldset>
  16. </form>
  17.  
  18. <form action="${pageContext.request.contextPath }/myController/requestint2.do">
  19. <fieldset>
  20. <legend>两个参数</legend>
  21. <input type="text" name="i" id="i"/>
  22. <input type="text" name="id" id="id"/>
  23. <input type="submit" value="提交">
  24. </fieldset>
  25. </form>
  26.  
  27. <form action="${pageContext.request.contextPath }/myController/requestint3.do">
  28. <fieldset>
  29. <legend>参数为一个对象</legend>
  30. <input type="text" name="id" id="id"/>
  31. <input type="text" name="username" id="username"/>
  32. <input type="password" name="password" id="password"/>
  33. <input type="text" name="age" id="age"/>
  34. <input type="submit" value="提交">
  35. </fieldset>
  36. </form>
  37.  
  38. <form action="${pageContext.request.contextPath }/myController/requestint4.do">
  39. <fieldset>
  40. <legend>参数为一个封装对象</legend>
  41. <input type="text" name="user.id" id="id"/>
  42. <input type="text" name="user.username" id="username"/>
  43. <input type="password" name="user.password" id="password"/>
  44. <input type="text" name="user.age" id="age"/>
  45. <input type="submit" value="提交">
  46. </fieldset>
  47. </form>
  48. </body>
  49. </html>

7、跳转的index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3. "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>springMVC_demo</title>
  8. </head>
  9. <body>
  10. xiaostudy
  11. </body>
  12. </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. 华为交换机忘记console的密码,怎么恢复出厂设置

    第一步:一般情况下由于密码忘记我们会被阻挡在交换机telnet远程管理界面之外. 第二步:虽然可以尝试console线连接交换机的控制接口,但是很多时候这个密码也被网络管理员进行了设置,不巧的是如果这 ...

  2. vue下给title配置图标.ico

    在根目录下放入要作为浏览网站时看到的网页title里的图标.如 32*32 后缀为.ico的图 然后再项目中build文件夹中的,webpack.dev.conf.js文件加入一句代码,加入完重启即可 ...

  3. Gunner II--hdu5233(map&vector/二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 ...

  4. Apache的访问控制

      目录配置段 注释不能写在指令后面,下面这样是不行的,应当换行,但为了阅读方便我就这么写了 Alias /dir/  "/var/www/html/admin"      #路径 ...

  5. Centos7编译4.7.2内核

    由于想要编译kvm-kmod,编译的时候提示内核版本太低,所以就不得不升级下linux内核,目前最新版内核是4.7.2,编译内核并不是一件很难的事,但是这么久没编译过,还是有必要记录下这一过程. 首先 ...

  6. 源码包安装(Python mysql redis)

    一  源码包安装 (1)python3.6源码包安装 ./configure ------> 定制功能 make make install mysql 源码包 cmake make make i ...

  7. karma安装

    Last login: Sat Jun :: on ttys000 ➜ ~ cd /Users/wangyizhe/Projects/work/smartcmp/services/new-yacmp/ ...

  8. 《FTL之垃圾回收、写放大和OP 》总结

    来自 http://www.ssdfans.com/?p=1840: 写放大WA: 对空盘来说(未触发GC),写放大一般为1,即Host写入多少数据,SSD写入闪存也是多少数据量(这里忽略SSD内部数 ...

  9. vim高亮显示文本

    行列高亮设置 • 行高亮 " 设置高亮行的颜色,ctermbg设定背景色,ctermfg设定前景色 set cursorline hi CursorLine cterm=NONE cterm ...

  10. CCF 权限查询(模拟)

    试题编号: 201612-3 试题名称: 权限查询 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 授权 (authorization) 是各类业务系统不可缺少的组成部分,系统 ...