Spring表单标签和modelAttribute
来源:互联网 发布:java 自己实现线程池 编辑:程序博客网 时间:2024/06/08 13:30
版权声明:本文为博主原创文章,未经博主允许不得转载。
- web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/springMVC.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>springMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/springMVC.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springMVC</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:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
- <mvc:resources mapping="/resources/**" location="/"/>
- <mvc:annotation-driven/>
- <context:component-scan base-package="com.test.controller" />
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix"><value>/WEB-INF/pages/</value></property>
- <property name="suffix"><value>.jsp</value></property>
- </bean>
- </beans>
- public class Book {
- String name;
- String press;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPress() {
- return press;
- }
- public void setPress(String press) {
- this.press = press;
- }
- }
- @Controller
- @RequestMapping("/welcome")
- public class Hello {
- //处理index.jsp表单提交
- @RequestMapping(value="/show.do",method=RequestMethod.POST)
- public String show(@ModelAttribute("book")Book book,Map<String,Object>model){
- model.put("book",book);
- return "hello";
- }
- //我们不能直接访问使用Spring表单标签的JSP页面,要先通过controller处理modelAttribute和表单标签的绑定才能访问
- @RequestMapping(value="/new.do",method=RequestMethod.GET)
- public String login(Map model){
- Book book=new Book();
- model.put("book",book);
- return "index";
- }
- }
切记,path后跟着的名字要与对象的属性名字一样,否则绑定不了
index.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
- <%@ page import="com.test.entity.*" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Insert title here</title>
- </head>
- <body>
- <form:form modelAttribute="book" method="POST" action="show.do">
- <table>
- <tr>
- <td><form:input path="name"/></td>
- </tr>
- <tr>
- <td><form:input path="press"/></td>
- </tr>
- <tr>
- <td><input type="submit" value="O K"> </td>
- </tr>
- </table>
- </form:form>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Insert title here</title>
- </head>
- <body>
- bookName=${book.getName()},press=${book.getPress()}
- </body>
- </html>
阅读全文
0 0
- Spring表单标签和modelAttribute
- Spring表单标签和modelAttribute
- @ModelAttribute注解和SpringMVC表单modelAttribute属性
- Spring MVC---数据绑定和表单标签
- spring @ModelAttribute
- spring @ModelAttribute
- Spring-@ModelAttribute
- spring ModelAttribute
- spring @ModelAttribute
- spring-mvc @ModelAttribute 绑定表单数据到VO
- spring mvc表单form值自动传到javabean-注解@ModelAttribute
- Spring 笔记 - @SessionAttribution和@ModelAttribute的作用
- spring mvc 表单标签
- Spring 表单标签
- Spring MVC表单标签
- Spring MVC表单标签
- spring mvc 表单标签
- spring表单标签
- switch语句中的判断能否是byte,long或String
- nginx基本配置与参数说明
- 实现strcat
- Android开发笔记:RxJava学习使用
- 全志R16平台的tinav2.1系统关闭adb_shell的步骤(分色排版)
- Spring表单标签和modelAttribute
- Linux开机自启动脚本的总结
- linux高级权限suid|sgid|sticky
- bios读不到usb启动盘
- hdu4305Matrix-Tree理论
- celery 分布式实现
- 事务添加
- ACM总结
- 图标