网站推荐、资源下载等 | 个人网站

springMVC数据回显

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   <!-- <servlet-mapping>
31     <servlet-name>springmvc</servlet-name>
32     <url-pattern>/rest/*</url-pattern>
33   </servlet-mapping> -->
34   
35   <welcome-file-list>
36     <welcome-file>index.html</welcome-file>
37     <welcome-file>index.htm</welcome-file>
38     <welcome-file>index.jsp</welcome-file>
39     <welcome-file>default.html</welcome-file>
40     <welcome-file>default.htm</welcome-file>
41     <welcome-file>default.jsp</welcome-file>
42   </welcome-file-list>
43   
44 </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         <!-- <mvc:annotation-driven/> --><!-- 这个可以替代下面处理器映射器和处理器适配器,仅在注解情况下可用 -->
23         
24         <!-- 配置注解处理器映射器 功能:寻找执行类Controller -->
25         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
26         
27         <!-- 配置注解处理器适配器 功能:调用controller方法,执行controller -->
28         <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
29         
30         <!-- 配置sprigmvc视图解析器:解析逻辑试图 
31              后台返回逻辑试图:index
32             视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/index.jsp -->
33         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
34             <property name="prefix" value="/WEB-INF/"/>
35             <property name="suffix" value=".jsp"/>        
36         </bean>
37 </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、注解类

 1 package com.xiaostudy.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.ui.Model;
 5 import org.springframework.web.bind.annotation.PathVariable;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 
10 import com.xiaostudy.domain.User;
11 
12 @Controller//<bean class="com.xiaostudy.controller.MyController"/>
13 @RequestMapping(value="/myController")//访问该类的方法时,前面多这样一个路径
14 public class MyController {
15     
16 //    @RequestMapping("hello")//http://localhost:8080/demo2/hello.do
17 //    @RequestMapping("/hello")//http://localhost:8080/demo2/hello.do
18 //    @RequestMapping(value="/hello.do")//http://localhost:8080/demo2/hello.do
19 //    @RequestMapping(value="/hello.do",method=RequestMethod.GET)//http://localhost:8080/demo2/hello.do
20 //    @RequestMapping(value="/hello.do",method= {RequestMethod.GET,RequestMethod.POST})//http://localhost:8080/demo2/hello.do
21     public String print() {
22         return "index";
23     }
24     
25     @RequestMapping("hi")//http://localhost:8080/demo2/myController/hi.do
26     public String hello() {
27         return "index";
28     }
29     
30     @RequestMapping("requestint")//http://localhost:8080/demo2/myController/requestint.do
31     public String requestint(int id) {
32         System.out.println(id);
33         return "index";
34     }
35     
36     @RequestMapping("requestint_2")//http://localhost:8080/demo2/myController/requestint2.do
37     public String requestint_2(@RequestParam(value="id2",required=true)int id) {//value="id2"表示:更改参数别名,required=true表示:直接访问地址会报错,必须要转跳
38         System.out.println(id);
39         return "index";
40     }
41     
42     @RequestMapping("requestint2")//http://localhost:8080/demo2/myController/requestint2.do
43     public String requestint2(int id, int i) {
44         System.out.println(id + "   " + i);
45         return "index";
46     }
47     
48     @RequestMapping("requestint3")//http://localhost:8080/demo2/myController/requestint3.do
49     public String requestint3(User user) {
50         System.out.println(user);
51         return "index";
52     }
53     
54     @RequestMapping("requestint4")//http://localhost:8080/demo2/myController/requestint4.do
55     public String requestint4(CustomUser customUser) {
56         System.out.println(customUser);
57         return "index";
58     }
59     
60     @RequestMapping("xiaostudy")//http://localhost:8080/demo2/myController/xiaostudy.do
61     public String add() {
62         return "xiaostudy";
63     }
64     
65     @RequestMapping("ok")//http://localhost:8080/demo2/myController/ok.do
66     public String ok(Model model) {//Model作用:用于回显数据
67         User user = new User();
68         user.setId(2);
69         user.setUsername("xiaostudy");
70         user.setPassword("123456");
71         user.setAge(23);
72         model.addAttribute("user", user);
73         return "ok";
74     }
75     
76     @RequestMapping("id/{id}")//http://localhost:8080/demo2/myController/id/?.do
77     public String id(@PathVariable int id) {//@PathVariable:说明{id}是对参数的id
78         System.out.println(id);
79         return "index";
80     }
81     
82     @RequestMapping("test")//http://localhost:8080/demo2/myController/test.do
83     public String test() {
84         return "forward:index";//转发
85     }
86     
87     @RequestMapping("test2")//http://localhost:8080/demo2/myController/test2.do
88     public String test2() {
89         return "redirect:index";//重定向
90     }
91 
92 }

5、回显数据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     ${user.id }||${user.username }||${user.password }||${user.age }<br/>
11     <a href="${pageContext.request.contextPath }/myController/id/${user.id}.do">id</a><br/>
12     <%-- <a href="${pageContext.request.contextPath }/rest/myController/id/${user.id}">id2</a><br/> --%>
13 </body>
14 </html>

项目文件结构


 

posted @ 2018-09-06 21:49  xiaostudy  阅读(508)  评论(0编辑  收藏  举报
网站推荐
[理工最爱]小时百科 |  GitHub |  Gitee |  开源中国社区 |  牛客网 |  不学网论坛 |  r2coding |  冷熊简历 |  爱盘 |  零散坑 |  bootstrap中文网 |  vue.js官网教程 |  源码分享站 |  maven仓库 |  楼教主网站 |  廖雪峰网站 |  w3cschool |  在线API |  代码在线运行 |  [不学网]代码在线运行 |  JS在线运行 |  PHP中文网 |  深度开源eclipse插件 |  文字在线加密解密 |  菜鸟教程 |  慕课网 |  千图网 |  手册网 |  素材兔 |  盘多多 |  悦书PDF |  sumatra PDF |  calibre PDF |  Snipaste截图 |  shareX截图 |  vlc-media-player播放器 |  MCMusic player |  IDM下载器 |  格式工厂 |  插件网 |  谷歌浏览器插件 |  Crx搜搜 |  懒人在线计算器 |  leetcode算法题库 |  layer官网 |  layui官网 |  formSelects官网 |  Fly社区 |  程序员客栈 |  融云 |  华为云 |  阿里云 |  ztree官网API |  teamviewer官网 |  sonarlint官网 |  editormd |  pcmark10官网 |  crx4chrome官网 |  apipost官网 |  花生壳官网 |  serv-u官网 |  杀毒eset官网 |  分流抢票bypass官网 |  懒猴子CG代码生成器官网 |  IT猿网 |  natapp[内网穿透] |  ngrok[内网穿透] |  深蓝穿透[内网穿透] |  WakeMeOnLan[查看ip] |  iis7 |  [漏洞扫描]Dependency_Check官网 |  [图标UI]fontawesome官网 |  idea插件官网 |  路过图床官网 |  sha256在线解密 |  在线正则表达式测试 |  在线文件扫毒 |  KuangStudy | 
资源下载
电脑相关: Windows原装下载msdn我告诉你 |  U盘制作微PE工具官网下载 |  Linux_CentOS官网下载 |  Linux_Ubuntu官网下载 |  Linux_OpenSUSE官网下载 |  IE浏览器官网下载 |  firefox浏览器官网下载 |  百分浏览器官网下载 |  谷歌google浏览器历史版本下载 |  深度deepin系统官网下载 |  中兴新支点操作系统官网下载 |  文件对比工具Beyond Compare官网下载 |  开机启动程序startup-delayer官网下载 |  openoffice官网下载 |  utorrent官网下载 |  qbittorrent官网下载 |  cpu-z官网下载 |  蜘蛛校色仪displaycal官网下载 |  单文件制作greenone下载 |  win清理工具Advanced SystemCare官网下载 |  解压bandizip官网下载 |  内存检测工具memtest官网下载 |  磁盘坏道检测与修复DiskGenius官网下载 |  磁盘占用可视化SpaceSniffer官网下载 |  [磁盘可视化]WizTree官网下载 |  win快速定位文件Everything官网下载 |  文件定位listary官网下载 |  动图gifcam官网下载 |  7-Zip官网下载 |  磁盘分区工具diskgenius官网下载 |  CEB文件查看工具Apabi Reader官网下载 |  罗技鼠标options官网下载 |  [去除重复文件]doublekiller官网下载 | 
编程相关: ApacheServer官网下载 |  Apache官网下载 |  Git官网下载 |  Git高速下载 |  Jboss官网下载 |  Mysql官网下载 |  Mysql官网历史版本下载 |  NetBeans IDE官网下载 |  Spring官网下载 |  Nginx官网下载 |  Resin官网下载 |  Tomcat官网下载 |  jQuery历史版本下载 |  nosql官网下载 |  mongodb官网下载 |  mongodb_linux历史版本下载 |  mongodb客户端下载 |  VScode官网下载 |  cxf官网下载 |  maven官网下载 |  QT官网下载 |  SVN官网下载 |  SVN历史版本下载 |  nodeJS官网下载 |  oracle官网下载 |  jdk官网下载 |  STS官网下载 |  STS历史版本官网下载 |  vue官网下载 |  virtualbox官网下载 |  docker desktop官网下载 |  github desktop官网下载 |  EditPlus官网下载 |  zTree下载 |  layui官网下载 |  jqgrid官网下载 |  jqueryui官网下载 |  solr历史版本下载 |  solr分词器ik-analyzer-solr历史版本下载 |  zookeeper历史版本官网下载 |  nssm官网下载 |  elasticsearch官网下载 |  elasticsearch历史版本官网下载 |  redis官网下载 |  redis历史版本官网下载 |  redis的win版本下载 |  putty官网下载 |  查看svn密码TSvnPD官网下载 |  MongoDB连接工具Robo官网下载 |  dll查看exescope官网下载 |  dll2c官网下载 |  接口测试apipost官网下载 |  接口测试postman官网下载 |  原型设计工具AxureRP官网下载 |  canal官网下载 |  idea主题样式下载 |  vue的GitHub下载 |  finalShell官网下载 |  ETL工具kafka官网下载 |  cavaj[java反编译]官网下载 |  jd-gui[java反编译]官网下载 |  radmin[远程连接]官网下载 |  tcping[win ping端口]下载 |  jQueryUploadFile官网下载 |  RedisPlus下载 |  aiXcoder智能编程助手官网下载 |  [表单效验]validform官网下载 |  idea官网下载 |  RedisStudio下载 |  MD转word含公式pandoc官网下载 |  logviewer官网下载 |  Kafka官网下载 |  hbase高速下载 |  hadoop官网下载 |  hadooponwindows的GitHub下载 |  hive官网下载 |  soapui官网下载 |  flink官网下载 |  kafkatool官网下载 |  MinIO官网下载 |  MinIO中国镜像下载 | 
办公相关工具
免费在线拆分PDF【不超过30M】 |  免费在线PDF转Word【不超过10M】 |  在线文字识别转换【不超过1M】 |  PDF转换成Word【不超过50M】 |  在线OCR识别 |  Smallpdf |  文件转换器Convertio |  迅捷PDF转换器 |  字母大小写转换工具 |  档铺 |  快传airportal[可文字] |  快传-文叔叔 |  P2P-小鹿快传 |  [图床]ImgURL | 
网站入口
腾讯文档 |  有道云笔记网页版 |  为知笔记网页版 |  印象笔记网页版 |  蓝奏云 |  QQ邮箱 |  MindMaster在线思维导图 |  bilibili |  PDM文件在线打开 |  MPP文件在线打开 |  在线PS软件 |  在线WPS |  阿里云企业邮箱登陆入口 | 
其他
PDF转换 |  悦书PDF转换 |  手机号注册查询 |  Reg007 |  akmsg |  ip8_ip查询 |  ipip_ip查询 |  天体运行testtubegames |  测试帧率 |  在线网速测试 |