spring的springMVC的一个简单的例子

来源:互联网 发布:讲文明知礼仪 编辑:程序博客网 时间:2024/06/07 23:31

//action的代码

package cn.itcast.actions;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.pojos.Dep;
import cn.itcast.service.DealDaoService;

@Controller //指明这个类是一个控制器的类,处理请求的
@RequestMapping("/dep.do")  //这是指明这个控制器类只能处理emp.do的请求
public class DealAction {
 @Resource
 private DealDaoService service;
 @RequestMapping(params="p=regist")//这是指明这个方法处理p=regist的请求
 public String registDep(HttpServletRequest request){
  
  String depname = request.getParameter("depname");
  Dep dep = new Dep();
  dep.setDepname(depname);
  
  service.registDep(dep);
  
  return "/result.jsp";//这是返回到以这个字符窜为视图的页面
 }

}

 

//这是pojos

package cn.itcast.pojos;

public class Dep implements java.io.Serializable {
 private Integer depid;
 private String depname;
 public Dep() {
 }

 public Dep(String depname) {
  this.depname = depname;
 }

 public Integer getDepid() {
  return this.depid;
 }

 public void setDepid(Integer depid) {
  this.depid = depid;
 }

 public String getDepname() {
  return this.depname;
 }

 public void setDepname(String depname) {
  this.depname = depname;
 }

}

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="cn.itcast.pojos.Dep" table="dep" catalog="wenbin">
        <id name="depid" type="java.lang.Integer">
            <column name="depid" />
            <generator class="native" />
        </id>
        <property name="depname" type="java.lang.String">
            <column name="depname" length="20" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

package cn.itcast.pojos;


public class Emp implements java.io.Serializable {

 private Integer empid;
 private String empname;
 private Integer depid;


 public Emp() {
 }

 public Emp(String empname, Integer depid) {
  this.empname = empname;
  this.depid = depid;
 }


 public Integer getEmpid() {
  return this.empid;
 }

 public void setEmpid(Integer empid) {
  this.empid = empid;
 }

 public String getEmpname() {
  return this.empname;
 }

 public void setEmpname(String empname) {
  this.empname = empname;
 }

 public Integer getDepid() {
  return this.depid;
 }

 public void setDepid(Integer depid) {
  this.depid = depid;
 }

}

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="cn.itcast.pojos.Emp" table="emp" catalog="wenbin">
        <id name="empid" type="java.lang.Integer">
            <column name="empid" />
            <generator class="native" />
        </id>
        <property name="empname" type="java.lang.String">
            <column name="empname" length="20" not-null="true" />
        </property>
        <property name="depid" type="java.lang.Integer">
            <column name="depid" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

//还有就是dao

省略

 

//service

package cn.itcast.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.daos.DepDAO;
import cn.itcast.daos.EmpDAO;
import cn.itcast.pojos.Dep;

@Service("dealDaoService") @Transactional
public class DealDaoService {
 
 @Resource
 private EmpDAO empDAO;
 @Resource
 private DepDAO depDAO;
 
 
 public void registDep(Dep dep)
 {
  depDAO.merge(dep);
 }
}

 

//applicationContext.xml

<?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"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
   http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd
   http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd" >
  

 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation"
   value="classpath:hibernate.cfg.xml">
  </property>
 </bean>
 <bean id="DepDAO" class="cn.itcast.daos.DepDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="EmpDAO" class="cn.itcast.daos.EmpDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <!-- <context:component-scan base-package="cn.itcast"></context:component-scan>
 <context:annotation-config/> -->
 <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>  
 <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
 
</beans>

 

//hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
 <property name="connection.username">root</property>
 <property name="connection.url">
  jdbc:mysql://localhost:3306/wenbin
 </property>
 <property name="dialect">
  org.hibernate.dialect.MySQLDialect
 </property>
 <property name="myeclipse.connection.profile">mysql</property>
 <property name="connection.password">caohuan</property>
 <property name="connection.driver_class">
  com.mysql.jdbc.Driver
 </property>
 <property name="show_sql">true</property>
 <mapping resource="cn/itcast/pojos/Dep.hbm.xml" />
 <mapping resource="cn/itcast/pojos/Emp.hbm.xml" />

</session-factory>

</hibernate-configuration>

 

//web.xml

<?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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!--这是配置核心控制器  -->
  <servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <!--这是配置启动spring容器的监听器  -->
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--这是防止session关闭的过滤器  -->
  <filter>
   <filter-name>openSessionInView</filter-name>
   <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>openSessionInView</filter-name>
 <url-pattern>/*</url-pattern> 
   </filter-mapping>
   <!--这是配置读取applicationContext.xml的路径  -->
   <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:app*.xml</param-value>
   </context-param>
   <!-- 这是处理中文的过滤器 -->
   <filter>
      <filter-name>chineaseFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>chineaseFilter</filter-name>
    <url-pattern>/*</url-pattern>  
   </filter-mapping>
</web-app>

//springMVC-servlet.xml

<?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:util="http://www.springframework.org/schema/util"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.0.xsd
 http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
   
 
    <!-- 让spring扫描业务包 -->
    <context:component-scan base-package="cn.itcast"></context:component-scan>
   
    <!-- 开启注解 -->
   <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>
 
</beans>

 

//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 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>
     <form action="${pageContext.request.contextPath}/dep.do?p=regist" method="post">
      部门名字:<input type="text" name="depname"/><br/>
      <input type="submit" value="提交"/>
     </form>
  </body>
</html>

 

原创粉丝点击