SpringMVC学习二

news/2024/7/1 4:50:47

使用POJO作为参数

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd"><display-name></display-name>    <!-- 配置org.springframework.web.filer.HiddenHttpMethodFilter:可以把POST请求转为DELETE或POST请求--><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><!-- 过滤所有请求 --><url-pattern>/*</url-pattern></filter-mapping><!-- 配置DispatcherServlet --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置 DispatcherServlet的一个初始化参数:配置SpringMvc 配置文件的位置和名称--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!-- 也可以不通过contextConfigLocation 来配置SpringMvc的配置文件,而使用默认的默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml/WEB-INF/springDispatcherServlet-servlet.xml--><!-- 设置启动 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><!-- 请求处理 --><servlet-name>springDispatcherServlet</servlet-name><!-- /:应答所有请求 --><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="
 http://www.springframework.org/schema/beans    
 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/mvc      
 http://www.springframework.org/schema/tx/spring-mvc-4.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!-- 配置自定义扫描包 --><context:component-scan base-package="como.springmvc.handlers"></context:component-scan><!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 前缀 --><property name="prefix" value="/WEB-INF/views/"></property><!-- 后缀 --><property name="suffix" value=".jsp"></property></bean></beans>

实体类代码:

User.java

package com.springmvc.entity;public class User {private String username;private String password;private String email;private int age;private Address address;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 String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}@Overridepublic String toString() {return "User [username=" + username + ", password="+ password + ", email=" + email + ", age=" + age+ ", address=" + address + "]";}}

Address.java

package com.springmvc.entity;public class Address {private String province;private String city;public String getProvince() {return province;}public void setProvince(String province) {this.province = province;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}@Overridepublic String toString() {return "Address [province=" + province + ", city=" + city + "]";}}

Pojo.java

package como.springmvc.handlers;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springmvc.entity.User;

@Controller
@RequestMapping("/Pojo")
public class Pojo {
    
    /*
     * SpringMVC会按请求参数名和POJO属性名进行自动匹配
     * 自动为该对象填充属性,支持级联属性
     * 如:dept.deptId、dept.address.tel
    
     * */
            
    @RequestMapping("/testPojo")
            public String testPojo(User user){
                    System.out.println("testPojo"+user);
                
                return "success";
                
            }
    
    /*
     * 可以使用Servlet原生的API作为目标方法的参数,具体支持一下类型
     * HttpServletRequest
     * HttpServletResponse
     * HttpSession
     * java.security.Principal
     * Locale InputStream
     * OutputStream
     * Reader
     * Writer
    
     * */
    
    @RequestMapping("/testServletAPI")
            public void testServletAPI(HttpServletRequest request,
                    HttpServletResponse response,Writer out) throws IOException{
                        System.out.println("testServletAPI,"+request+","+response);
                        out.write("hello");
                //return "success";
                
            }
}

pojo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'pojo.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    
  </head>
 
  <body>
  <form action="Pojo/testPojo"  method="post"><br>
          username:<input type="text"  name="username" /><br>
          password:<input type="password"  name="password" /><br>
          email:<input type="text"  name="email" /><br>
          age:<input type="text"  name="age" /><br>
          <!-- 级联属性 :属性的属性 -->
          city:<input type="text"  name="address.city" /><br>
          province:<input type="text"  name="address.province" /><br>
          <input type="submit" value="Value" />
 
  </form>
    
    
    <a href="Pojo/testServletAPI">Test ServletAPI</a>
  </body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"  isErrorPage="true"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'success.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>success page. <br></body>
</html>

 

转载于:https://www.cnblogs.com/liurg/p/8005782.html


http://lihuaxi.xjx100.cn/news/238079.html

相关文章

考csp所需算法_CSP vs RxJS:您所不知道的。

考csp所需算法by Kevin Ghadyani通过凯文加迪亚尼(Kevin Ghadyani) CSP vs RxJS&#xff1a;您所不知道的。 (CSP vs RxJS: what you don’t know.) CSP发生了什么&#xff1f; (What happened to CSP?) You probably clicked this article thinking “what is CSP?” It’s…

哈希函数是什么,在区块链中有什么用?

想知道更多关于区块链技术知识&#xff0c;请百度【链客区块链技术问答社区】 链客&#xff0c;有问必答&#xff01;哈希函数是什么&#xff1f; 哈希函数&#xff0c;又叫散列函数、散列算法&#xff0c;是一种从任何一种数据中创建小的数字“指纹”&#xff08;也叫做摘要&a…

深入学习Lock锁(2)——LockSupport工具类

2019独角兽企业重金招聘Python工程师标准>>> 在同步组件中&#xff0c;当需要阻塞或唤醒一个线程的时候&#xff0c;都会使用LockSupport工具类来完成相应 工作。LockSupport定义了一组的公共静态方法&#xff0c;这些方法提供了最基本的线程阻塞和唤醒功能&#xf…

自动化运维工具Saltstack(一)

1、saltstack简介&#xff1a; 什么是saltstack&#xff1f; saltstack是基于python开发的一套C/S架构配置管理工具 使用SSL证书签方的方式进行认证管理 号称世界上最快的消息队列ZeroMQ使得SaltStack能快速在成千上万台机器上进行各种操作 采用RSA Key方式确认身份 传输采用AE…

JavaScript词法作用域的简单介绍

by Michael McMillan迈克尔麦克米兰(Michael McMillan) JavaScript词法作用域的简单介绍 (An easy intro to Lexical Scoping in JavaScript) Lexical scoping is a topic that frightens many programmers. One of the best explanations of lexical scoping can be found in…

非对称加密及RSA算法

想知道更多关于区块链技术知识&#xff0c;请百度【链客区块链技术问答社区】 链客&#xff0c;有问必答&#xff01;最近在学习区块链相关的知识&#xff0c;发现其保证去中心化的一个重要的手段就是基于密码学中的非对称加密。 何为非对称加密&#xff1f; 在回答这个问题之前…

处理器拦截器(HandlerInterceptor)详解

处理器拦截器&#xff08;HandlerInterceptor&#xff09;详解 编程界的小学生 关注 2017.04.06 15:19* 字数 881 阅读 657评论 0喜欢 4简介SpringWebMVC的处理器拦截器&#xff0c;类似于Servlet开发中的过滤器Filter&#xff0c;用于处理器进行预处理和后处理。 应用场景1、日…

saltstack实现haproxy+keepalived负载均衡+高可用(二)

一键部署haproxykeepalived实现负载均衡高可用 实验环境&#xff1a; &#xff01;&#xff01;&#xff01;&#xff01; 特别注意&#xff1a; www.westos.org为test1的minion名字 test1: 172.25.1.11 nginx master minion test2: 172.25.…