Java 获取POST数据以及简单模板输出


Java获取POST数据

一般在开发的时候,我们需要获取表单中提交的数据,那么我们必须先创建一个say.jsp,这个jsp的内容是一个非常简单的表单,POST方式提交,提交到/hello/show/路径上。

  • <form action='/hello/show/' method="post"
  • <input name="username" type="text" /> 
  • <input name="password" type="text" /> 

  1. <input name="" type="submit" /> 
  2. </form> 

然后我们需要一个控制器文件,两个Action,一个是现实say.jsp静态页面,一个是接收处理POST提交过来的数据。

其中sya()方法显示静态页面;show()方法处理POST数据。

show方法两个参数,User user这个对象 Spring会自动将POST数据填充到User这个类上面去,Modelmodel主要用来实现Controller和模板之间数据传递。

  1. package com.mvc.rest; 
  2.  
  3. import org.springframework.stereotype.Controller; 
  4. import org.springframework.ui.Model; 
  5. import org.springframework.web.bind.annotation.RequestMapping; 
  6.  
  7. //@Controller 是一个标识这个是控制器类的注解标签,如果是控制器类 都需要有这个注解。  
  8. @Controller 
  9. //@RequestMapping(value="/hello") 会映射到url /hello/则访问HelloController中的Action  
  10. @RequestMapping(value="/hello"
  11. public class HelloController { 
  12.      
  13.     //@RequestMapping(value="/say") 会映射到url /hello/say则访问HelloController中的Action  
  14.     @RequestMapping(value="/say"
  15.     public void say() { 
  16.         System.out.print("this is HelloController And say Action \r\n"); 
  17.     } 
  18.      
  19.     @RequestMapping("/show"
  20.     public String show(User user,Model model) { 
  21.         System.out.print(user.getUsername()); 
  22.         System.out.print(user.getPassword()); 
  23.         model.addAttribute("user", user); 
  24.         return "hello/show"
  25.     } 

User类:

  1. package com.mvc.rest; 
  2.  
  3. public class User { 
  4.     private String username; 
  5.      
  6.     private String password; 
  7.  
  8.     public String getUsername() { 
  9.         return username; 
  10.     } 
  11.  
  12.     public void setUsername(String username) { 
  13.         this.username = username; 
  14.     } 
  15.  
  16.     public String getPassword() { 
  17.         return password; 
  18.     } 
  19.  
  20.     public void setPassword(String password) { 
  21.         this.password = password; 
  22.     } 
  23.      
  24.  

show.jsp模板类,输出接收到的POST数据:

  1. user:${user.username}
  2. password:${user.password} 

bean.xml配置:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans   
  3.     xmlns="http://www.springframework.org/schema/beans"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xmlns:context="http://www.springframework.org/schema/context" 
  6.     xmlns:aop="http://www.springframework.org/schema/aop" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
  9.     http://www.springframework.org/schema/context 
  10.     http://www.springframework.org/schema/context/spring-context-2.5.xsd 
  11.     http://www.springframework.org/schema/aop 
  12.     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
  13.     ">   
  14.     <context:annotation-config/> 
  15.     <aop:aspectj-autoproxy/>   
  16.      <bean id="User" class="com.mvc.rest.User" ></bean> 
  17. </beans> 

相关内容