项目结构
2 web.xml的配置内容如下:
<?xmlversion="1.0"encoding="utf-8"?>
<web-appversion="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">
<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>
<!-- struts用/*,springmvc不能/*,语法
*.xxx -->
<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的配置内容如下:
<beansxmlns="http://www.springframework.org/schema/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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<context:component-scanbase-package="com.rl.controller"/>
<bean
class="org.springframework.web.servlet.view.internalresourceviewresolver">
<propertyname="prefix"value="/web-inf/jsp/"></property>
<propertyname="suffix"value=".jsp"></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[%t]%-5p[%c]-%m%n
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的配置内容如下:
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scanbase-package="com.rl"/>
<beanid="datasource"class="org.springframework.jdbc.datasource.drivermanagerdatasource">
<propertyname="driverclassname"value="com.mysql.jdbc.driver"></property>
<propertyname="url"value="jdbc:mysql://localhost:3306/springmvc"></property>
<propertyname="username"value="root"></property>
<propertyname="password"value="123456"></property>
<beanid="sessionfactory"class="org.springframework.orm.hibernate3.localsessionfactorybean">
<propertyname="datasource"ref="datasource"></property>
<propertyname="hibernateproperties">
<props>
<propkey="hibernate.dialect">org.hibernate.dialect.mysql5dialect</prop>
<propkey="hibernate.show_sql">true</prop>
<propkey="hibernate.hbm2ddl">update</prop>
</props>
</property>
<propertyname="mappingdirectorylocations"value="classpath:com/rl/mapping/"></property>
<beanid="txmanager"class="org.springframework.orm.hibernate3.hibernatetransactionmanager">
<propertyname="sessionfactory"ref="sessionfactory"></property>
<tx:adviceid="txadvice"transaction-manager="txmanager">
<tx:attributes>
<tx:methodname="save*"propagation="required"/>
<tx:methodname="get*"read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisoradvice-ref="txadvice"pointcut="execution(*
com.rl.service..*.*(..))"/>
</aop:config>
person0420的代码如下:
package com.rl.model;
import java.util.date;
@suppresswarnings("serial")
publicclass
person0420implements java.io.serializable {
private
integerpersonid;
stringname;
stringgender;
stringaddress;
datebirthday;
/**
*
*/
public
person0420() {
}
*@param
personid
name
gender
address
birthday
person0420(integer personid, string name, string gender,
string address, date birthday) {
this.personid
= personid;
this.name
= name;
this.gender
= gender;
this.address
= address;
this.birthday
= birthday;
*@return
the personid
integer getpersonid() {
returnpersonid;
personid the personid to set
publicvoid
setpersonid(integer personid) {
the name
string getname() {
returnname;
name the name to set
setname(string name) {
the gender
string getgender() {
returngender;
gender the gender to set
setgender(string gender) {
the address
string getaddress() {
returnaddress;
address the address to set
setaddress(string address) {
the birthday
date getbirthday() {
returnbirthday;
birthday the birthday to set
setbirthday(date birthday) {
}
person0420.hbm.xml的内容如下:
<!doctypehibernate-mappingpublic"-//hibernate/hibernate
mapping dtd 3.0//en"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
mapping fileautogenerated by myeclipse persistence tools
-->
<hibernate-mapping>
<classname="com.rl.model.person0420"table="person_0420"catalog="springmvc">
<idname="personid"type="java.lang.integer">
<columnname="person_id"/>
<generatorclass="identity"/>
</id>
<propertyname="name"type="java.lang.string">
<columnname="name"length="10"/>
<propertyname="gender"type="java.lang.string">
<columnname="gender"length="1"/>
<propertyname="address"type="java.lang.string">
<columnname="address"length="50"/>
<propertyname="birthday"type="java.util.date">
<columnname="birthday"length="0"/>
</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;
publicinterfacepersondao
{
save(person0420 person);
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;
@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;
publicinterface
personservice {
personserviceimpl的内容如下:
package com.rl.service.impl;
import org.springframework.stereotype.service;
import com.rl.service.personservice;
@service
public class personserviceimpl implements personservice {
persondao persondao;
persondao.save(person);
personcontroller的内容如下:
package com.rl.controller;
import java.text.simpledateformat;
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;
@controller
@requestmapping("/person")
public class personcontroller {
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的内容如下:
<%@pagelanguage="java"import="java.util.*"pageencoding="utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctypehtmlpublic
"-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<basehref="<%=basepath%>">
<title>my
jsp 'index.jsp' starting page</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="this
is my page">
<!--
<linkrel="stylesheet" type="text/css"
href="styles.css">
-->
</head>
<body>
<formaction="person/save.do"method="post">
name:<inputname="name"type="text"><br>
gender:<inputname="gender"type="text"><br>
address:<inputname="address"type="text"><br>
birthday:<inputname="birthday"type="text"><br>
<inputvalue="submit"type="submit">
</form>
</body>
</html>
success.jsp的内容如下:
<%@pagelanguage="java"import="java.util.*"pageencoding="iso-8859-1"%>
success<br>
浏览器中的访问地址是:http://localhost:8080/ssh/person/toform.do