请给出一个SpringMVC的表单提交的例子和session运用的例子
- 2019 年 10 月 10 日
- 筆記
2.表单提交和session 像学servlet那时一样,继hello world的例子以后,紧接着我们就要学习表单提交和session。 例2.1 <%@ page contentType="text/html; charset=GBK" %> <html> <head> <title>form test</title> </head> <body> <%=session.getAttribute("firstN") %> <FORM ACTION="formHan.do" METHOD="POST"> 姓名: <INPUT TYPE="TEXT" NAME="firstName"><BR> <INPUT TYPE="SUBMIT" VALUE="Submit"> </CENTER> </FORM> </body> </html> package com; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloWorldController { @RequestMapping("/formHan") public void formHandle(HttpServletRequest req, HttpServletResponse res, HttpSession ses) throws IOException, ServletException { //没用 req.setCharacterEncoding("gbk"); String fn=req.getParameter("firstName"); System.out.println(fn+"1"); String fngbk = new String(fn.getBytes("iso8859-1"), "GBK"); System.out.println("filenameutf is " + fngbk); ses.setAttribute("firstN", fngbk); // PrintWriter pw=res.getWriter(); // pw.println(fn);//此句可以工作 req.getRequestDispatcher("formT.jsp").forward(req, res); // res.sendRedirect("formT.jsp");//此句可以工作 // return "/formT";//此句可以工作 } }
更多请见:https://blog.csdn.net/qq_44639795/article/details/100185314