- 项目结构
2
web.xml的配置内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee <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> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- struts用/*, <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> </web-app> |
- springmvc.xml的配置内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans <context:component-scan <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property <property </bean> </beans> |
- log4j.properties的内容如下:
log4j.rootLogger=DEBUG, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG |
- beans.xml的配置内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans <context:component-scan <bean <property <property <property <property </bean> <bean <property <property <props> <prop <prop <prop </props> </property> <property </bean> <bean <property </bean> <tx:advice <tx:attributes> <tx:method <tx:method </tx:attributes> </tx:advice> <aop:config> <aop:advisor </aop:config> </beans> |
- Person0420的代码如下:
package com.rl.model; import java.util.Date; @SuppressWarnings("serial") public private Integer private String private String private String private Date /** * */ public Person0420() { } /** * @param personId * @param name * @param gender * @param address * @param birthday */ public Person0420(Integer personId, String name, String address, Date birthday) { this.personId this.name this.gender this.address this.birthday } /** * @return the */ public Integer getPersonId() { return } /** * @param personId */ public this.personId } /** * @return the */ public String getName() { return } /** * @param name */ public this.name } /** * @return the */ public String getGender() { return } /** * @param gender */ public this.gender } /** * @return the */ public String getAddress() { return } /** * @param address */ public this.address } /** * @return the */ public Date getBirthday() { return } /** * @param birthday */ public this.birthday } } |
- Person0420.hbm.xml的内容如下:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class <id <column <generator </id> <property <column </property> <property <column </property> <property <column </property> <property <column </property> </class> </hibernate-mapping> |
- 创建数据库和表所需的SQL语句:
DROP DATABASE springmvc; CREATE DATABASE springmvc DEFAULT CHARSET utf8; USE springmvc; CREATE TABLE person_0420( PERSON_ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(10) NOT NULL, GENDER VARCHAR(1) NOT NULL, ADDRESS VARCHAR(50) NOT NULL, birthday DATE ); |
- PersonDao的代码如下:
package com.rl.dao; import com.rl.model.Person0420; public public } |
- PersonDaoImpl 的内容如下:
package com.rl.dao.impl; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import com.rl.dao.PersonDao; import com.rl.model.Person0420; @Repository public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao { @Autowired public void setMySessionFactory(SessionFactory sessionFactory){ super.setSessionFactory(sessionFactory); } public void save(Person0420 person) { this.getHibernateTemplate().save(person); } } |
- PersonService的内容如下:
package com.rl.service; import com.rl.model.Person0420; public public } |
- PersonServiceImpl的内容如下:
package com.rl.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.rl.dao.PersonDao; import com.rl.model.Person0420; import com.rl.service.PersonService; @Service public class PersonServiceImpl implements PersonService { @Autowired PersonDao personDao; public void save(Person0420 person) { personDao.save(person); } } |
- PersonController的内容如下:
package com.rl.controller; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import com.rl.model.Person0420; import com.rl.service.PersonService; @Controller @RequestMapping("/person") public class PersonController { @Autowired PersonService personService; @RequestMapping("/toForm.do") public String toForm() { return "form"; } @RequestMapping("/save.do") public String save(Person0420 person) { personService.save(person); return "success"; } @InitBinder public void initBinder(ServletRequestDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd"), true)); } } |
14 form.jsp的内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE <html> <head> <base <title>My <meta <meta <meta <meta <meta <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form name:<input gender:<input address:<input birthday:<input <input </form> </body> </html> |
- success.jsp的内容如下:
<%@ 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> <head> <base <title>My <meta <meta <meta <meta <meta <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> success<br> </body> </html> |
浏览器中的访问地址是:http://localhost:8080/ssh/person/toForm.do