Spring MVC 06 - @RequestParam

@RequestParam 浏览器提交表单时,传入服务器的参数由@RequestParam携带

1. 承接上一篇的代码内容,在相同的包名下新建一个类StudentAdmissionController.java 

package com.haha;

import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StudentAdmissionController{
   
   @RequestMapping(value="/admissionForm.html",method = RequestMethod.GET)
   public ModelAndView getAdmissionForm(){
      ModelAndView model = new ModelAndView("AdmissionForm");
      return model;
   }

   @RequestMapping(value="/submitAdmissionForm.html", method = RequestMethod.POST)
   public ModelAndView submitAdmissionForm(@RequestParam Map<String,String> reqVar){
  String name = reqVar.get("studentName");
  String hobby = reqVar.get("studentHobby");
       ModelAndView model = new ModelAndView("AdmissionSuccess");
       model.addObject("msg","Details submitted by you::Name: "+ name+", Hobby: "+hobby);
       return model;
   }
}

2. 在WEB-INF目录下新建表单页面AdmissionSuccess.jsp

<html>
<body>
  <h1>STUDENT ADMISSION FROM FOR ENGINEERING COURSES</h1>
  <form action="/FirstSpringMVCPro/submitAdmissionForm.html" method="post">
     <p>
        Student's Name : <input type="text" name="studentName" />
     </p>
     <p>
        Student's Hobby : <input type="text" name="studentHobby" />
     </p>
     <input type="submit" value="Submit this form by clicking here"/>
  </form>
</body>
</html>

3. 在WEB-INF目录下新建表单提交成功后返回的页面AdmissionSuccess.jsp

<html>
<body>
  <h1>Congratulations!! the Engineering college has processed your Application form successfully</h1>
  <h2>${msg}</h2>
</body>
</html>

4. 用tomcat运行项目,在浏览器输入http://localhost:8080/FirstSpringMVCPro/admissionForm.html


输入内容后,点击提交按钮


猜你喜欢

转载自blog.csdn.net/u013537471/article/details/53014748
今日推荐