SpringMVC--提交表单
今天使用AbstractCommandController做一个提交表单的样例
(1)首先,建立一个User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.zk.domain; import java.util.Date; public class User { private Integer id; private String name; private String age; private String birthday; public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getAge() { return age; } public void setAge(String age) { this .age = age; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this .birthday = birthday; } } |
(2)第二步,实现ToAddController.java,跳转至add.jsp,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.zk.UserController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class ToAddController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub ModelAndView mv= new ModelAndView(); mv.setViewName( "add" ); return mv; } } |
(3)第三步,实现CommandController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.zk.UserController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractCommandController; import com.zk.domain.User; //从页面接受参数,封装javaBean的User对象 public class CommandController extends AbstractCommandController{ //指定把参数封装到那个对象。 public CommandController(){ this .setCommandClass(User. class ); } @Override protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { // TODO Auto-generated method stub //值被封装命令对象 User user=(User) command; ModelAndView mv= new ModelAndView(); //设置models mv.addObject( "user" ,user); //指定返回视图界面 mv.setViewName( "index" ); return mv; } } |
(4)第四步,配置SpringMVC.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 配置处理器映射器 --> <bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean> <bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" ></bean> <bean class = "org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" ></bean> <!-- 命令对象 --> <bean id= "/command.do" class = "com.zk.UserController.CommandController" ></bean> <!-- 跳转到Add页面 --> <bean id= "/toAdd.do" class = "com.zk.UserController.ToAddController" ></bean> <bean id= "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/jsp/" ></property> <property name= "suffix" value= ".jsp" ></property> </bean> </beans> |
(5)两个jsp界面
add.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <%@ 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 'add.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> <form action= "${pageContext.request.contextPath }/command.do" > Id:<input type= "text" name= "id" id= "id" /><br> name:<input type= "text" name= "name" id= "name" /><br> age:<input type= "text" name= "age" id= "age" /><br> birthday:<input type= "text" name= "birthday" id= "birthday" /><br> <input type= "submit" value= "提交" /> </form> </body> </html> |
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <%@ 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 'index.jsp' starting page</title> <meta http-equiv= "Content-Type" content= "text/html;charset=utf-8" /> <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> <!-- ${hello } --> <h1>${user.name}||${user.id}||${user.age}||${user.birthday}</h1> </body> </html> |
运行结果如下:
http://ms-20170731tkkc:8080/SpringMVC_004/command.do?id=1&name=zk&age=24&birthday=1995-03-04
带过来的参数
注解写法
Usercontroller.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.zk.Controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller //<bean id="UserController" class="UserController路径"> public class UserController { //访问路径注意更改 //请求映射value="/hello.do", //@RequestMapping("hello") //@RequestMapping("/hello.do") //@RequestMapping(value="/hello.do") //@RequestMapping(value="/hello.do",method=RequestMethod.GET) @RequestMapping (value= "/hello.do" ,method={RequestMethod.GET,RequestMethod.POST}) public String myHello(){ return "hello" ; } @RequestMapping ( "receive" ) public String receiveInt(Integer id){ System.out.println(id); return "success" ; } } |
SpringMVC.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "http://www.springframework.org/schema/context" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.0.xsd http: //www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http: //www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base- package = "com.zk.Controller" ></context:component-scan> <!-- 配置注解处理器映射器 功能:寻找执行类Controller --> <bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" ></bean> <!-- 配置注解处理器适配器 功能:调用controller方法,执行controller --> <bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" ></bean> <bean id= "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/jsp/" ></property> <property name= "suffix" value= ".jsp" ></property> </bean> </beans> |
hello.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <%@ 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 'Hello.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> Hello,UserController <br> <form action= "${pageContext.request.contextPath}/receive.do" method= "post" > ID:<input type= "text" name= "id" id= "id" /> <input type= "submit" value= "提交" /> </form> </body> </html> |
success.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <%@ page language= "java" import = "java.util.*" pageEncoding= "ISO-8859-1" %> <% 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 'index.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. <br> </body> </html> |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version= "1.0" encoding= "UTF-8" ?> <web-app version= "2.5" 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_2_5.xsd"> <display-name>Spring_006</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class > <!-- Spring 默认加载SpringMVC的配置文件,这个需要满足一下规则: 命名规则:Servlet-name-servlet.xml========>SpringMVC-servlet.xml 路径规范SpringMVC-serlvet.xml必须放在WEB-INF下 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*. do </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
运行结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)