SpringMvc项目的完整配置

uploadDir=upload


首先介绍下我的开发工具是:Eclipse,项目配置的机构如图:

 

二、在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>
  <!-- spring 配置文件的加载 -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/springmvc-servlet.xml</param-value>
  </context-param>
  <!-- 监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- springMVC的配置 -->
  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 加载springMVC的配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:config/springmvc-servlet.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>
  <!-- 过滤编码方式,防止乱码 -->
  <filter>
  	<filter-name>encodingFilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


三、配置springmvc文件,springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    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.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 加载包中的controller  注解扫描包 -->
   <context:component-scan base-package="net.zdsoft.eis"/>
   
   <!-- 开启注解 -->
   <context:annotation-config />
   
   <!-- 完成请求和注解POJO的映射 -->
   <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
   <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
   
   
   <!-- 静态资源的访问 -->
	<mvc:resources location="/img/" mapping="/img/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>
	
    <!--视图分解器    对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <!-- 一种方式
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
    -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- 上传文件的解析器 -->
	<!-- 
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"/>
		<property name="maxUploadSize" value="10485760000"/>
		<property name="maxInMemorySize" value="40960"/>
	</bean>
    -->
</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>
		<mapping resource="com/css/model/Emp.hbm.xml" />
	</session-factory>

</hibernate-configuration>

 

五、hibernate.properties数据库的字典内容如下:

dataSource.dialect=org.hibernate.dialect.OracleDialect
dataSource.driverClassName=oracle.jdbc.driver.OracleDriver
dataSource.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
dataSource.username=springmvc
dataSource.password=springmvc
dataSource.hbm2ddl.auto=update
dataSource.show_sql=true

 

六、springMVC.properties配置如下:

uploadDir=upload

 

七、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: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">
	<context:component-scan base-package="com.css.*" />
	<context:property-placeholder location="classpath:/hibernate.properties" />
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName">
			<value>${dataSource.driverClassName}</value>
		</property>
		<property name="username">
			<value>${dataSource.username} </value>
		</property>
		<property name="password">
			<value>${dataSource.password} </value>
		</property>
		<property name="url">
			<value>${dataSource.url}</value>
		</property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:hibernate.cfg.xml" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${dataSource.dialect}</prop>
				<prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop>
				<prop key="hibernate.show_sql">${dataSource.show_sql}</prop>
			</props>
		</property>
	</bean>
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!--配置一个JdbcTemplate实例 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
</beans>


八、实体类Emp.java的内容如下:

package com.css.model;

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


/**
 * Emp entity. @author MyEclipse Persistence Tools
 */
@Entity
public class Emp implements java.io.Serializable {

	// Fields
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Short empno;
	private String ename;
	private String job;
	private Short mgr;
	private Timestamp hiredate;
	private Double sal;
	private Double comm;
	private Byte deptno;

	// Constructors

	/** default constructor */
	public Emp() {
	}

	/** full constructor */
	public Emp(String ename, String job, Short mgr, Timestamp hiredate,
			Double sal, Double comm, Byte deptno) {
		this.ename = ename;
		this.job = job;
		this.mgr = mgr;
		this.hiredate = hiredate;
		this.sal = sal;
		this.comm = comm;
		this.deptno = deptno;
	}

	// Property accessors

	此处省略set和get方法......
}


九、实体类的配置文件Emp.hbm.xml内容如下:

<?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="com.css.model.Emp" table="EMP" schema="SPRINGMVC">
        <id name="empno" type="java.lang.Short">
            <column name="EMPNO" precision="4" scale="0" />
            <generator class="sequence" />
        </id>
        <property name="ename" type="java.lang.String">
            <column name="ENAME" length="10" />
        </property>
        <property name="job" type="java.lang.String">
            <column name="JOB" length="9" />
        </property>
        <property name="mgr" type="java.lang.Short">
            <column name="MGR" precision="4" scale="0" />
        </property>
        <property name="hiredate" type="java.sql.Timestamp">
            <column name="HIREDATE" length="7" />
        </property>
        <property name="sal" type="java.lang.Double">
            <column name="SAL" precision="7" />
        </property>
        <property name="comm" type="java.lang.Double">
            <column name="COMM" precision="7" />
        </property>
        <property name="deptno" type="java.lang.Byte">
            <column name="DEPTNO" precision="2" scale="0" />
        </property>
    </class>
</hibernate-mapping>

 

十、DAO的实现方法EmpDAO.java 内容如下:

package com.css.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.css.model.Emp;


@Repository("empDAO")
public class EmpDAO {
	@Resource
	private HibernateTemplate hibernateTemplate;
	
	

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	
	
	private static final Logger log = LoggerFactory.getLogger(EmpDAO.class);
	// property constants
	public static final String ENAME = "ename";
	public static final String JOB = "job";
	public static final String MGR = "mgr";
	public static final String SAL = "sal";
	public static final String COMM = "comm";
	public static final String DEPTNO = "deptno";

	protected void initDao() {
		// do nothing
	}

	public void save(Emp transientInstance) {
		log.debug("saving Emp instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Emp persistentInstance) {
		log.debug("deleting Emp instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Emp findById(java.lang.Short id) {
		log.debug("getting Emp instance with id: " + id);
		try {
			Emp instance = (Emp) getHibernateTemplate().get("com.css.dao.Emp",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Emp instance) {
		log.debug("finding Emp instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Emp instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Emp as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByEname(Object ename) {
		return findByProperty(ENAME, ename);
	}

	public List findByJob(Object job) {
		return findByProperty(JOB, job);
	}

	public List findByMgr(Object mgr) {
		return findByProperty(MGR, mgr);
	}

	public List findBySal(Object sal) {
		return findByProperty(SAL, sal);
	}

	public List findByComm(Object comm) {
		return findByProperty(COMM, comm);
	}

	public List findByDeptno(Object deptno) {
		return findByProperty(DEPTNO, deptno);
	}

	public List findAll() {
		log.debug("finding all Emp instances");
		try {
			String queryString = "from Emp";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Emp merge(Emp detachedInstance) {
		log.debug("merging Emp instance");
		try {
			Emp result = (Emp) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Emp instance) {
		log.debug("attaching dirty Emp instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Emp instance) {
		log.debug("attaching clean Emp instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static EmpDAO getFromApplicationContext(ApplicationContext ctx) {
		return (EmpDAO) ctx.getBean("EmpDAO");
	}
}


十一、service层的方法此处暂不列出了,方法名称内容如DAO层的内容。

 

十二、此处列出controller中的几个控制类的内容:

        

package com.css.contorller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

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

import com.css.service.ServiceS;
@Controller

public class Login {
	@Resource
	private ServiceS serviceS;
	@RequestMapping("/test.do")
	public ModelAndView query()
	{
		Map<String, Object> prames = new HashMap<String, Object>();
		prames.put("lists", serviceS.find());
		ModelAndView model = new ModelAndView("list",prames);
		return model;
	}
}

 

package net.zdsoft.eis.controller;

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

import net.zdsoft.eis.dao.User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;


@Controller  //类似Struts的Action
public class TestController {

    @RequestMapping("test/login.do")  // 请求url地址映射,类似Struts的action-mapping
    public String testLogin(@RequestParam(value="username")String username, String password, HttpServletRequest request) {
        // @RequestParam是指请求url地址映射中必须含有的参数(除非属性required=false)
        // @RequestParam可简写为:@RequestParam("username")

        if (!"admin".equals(username) || !"admin".equals(password)) {
            return "loginError"; // 跳转页面路径(默认为转发),该路径不需要包含spring-servlet配置文件中配置的前缀和后缀
        }
        return "loginSuccess";
    }

    @RequestMapping("/test/login2.do")
    public ModelAndView testLogin2(String username, String password, int age){
        // request和response不必非要出现在方法中,如果用不上的话可以去掉
        // 参数的名称是与页面控件的name相匹配,参数类型会自动被转换
        
        if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return new ModelAndView("loginError"); // 手动实例化ModelAndView完成跳转页面(转发),效果等同于上面的方法返回字符串
        }
        return new ModelAndView(new RedirectView("../index.jsp"));  // 采用重定向方式跳转页面
        // 重定向还有一种简单写法
        // return new ModelAndView("redirect:../index.jsp");
    }

    @RequestMapping("/test/login3.do")
    public ModelAndView testLogin3(User user) {
        // 同样支持参数为表单对象,类似于Struts的ActionForm,User不需要任何配置,直接写即可
        String username = user.getUsername();
        String password = user.getPassword();
        int age = user.getAge();
        
        if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return new ModelAndView("loginError");
        }
        return new ModelAndView("loginSuccess");
    }

//    @Resource(name = "loginService")  // 获取applicationContext.xml中bean的id为loginService的,并注入
//    private LoginService loginService;  //等价于spring传统注入方式写get和set方法,这样的好处是简洁工整,省去了不必要得代码

//    @RequestMapping("/test/login4.do")
//    public String testLogin4(User user) {
//        if (loginService.login(user) == false) {
//            return "loginError";
//        }
//        return "loginSuccess";
//    }
}
 
 
package net.zdsoft.eis.controller;

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

@Controller
@RequestMapping("/test2/login.do")  // 指定唯一一个*.do请求关联到该Controller
public class TestController2 {
    
    @RequestMapping
    public String testLogin(String username, String password, int age) {
        // 如果不加任何参数,则在请求/test2/login.do时,便默认执行该方法
        
        if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return "loginError";
        }
        return "loginSuccess";
    }

    @RequestMapping(params = "method=1", method=RequestMethod.POST)
    public String testLogin2(String username, String password) {
        // 依据params的参数method的值来区分不同的调用方法
        
        // 可以指定页面请求方式的类型,默认为get请求
        System.out.println(username);
        if (!"admin".equals(username) || !"admin".equals(password)) {
            return "loginError";
        }
        return "loginSuccess";
    }
    
    @RequestMapping(params = "method=2")
    public String testLogin3(String username, String password, int age) {
    	// eg: http://localhost:8080/test2/login.do?username=admin&password=admin&age=6&method=2
    	if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return "loginError";
        }
        System.out.println(username);
        return "loginSuccess";
    }
}

此处暂时列出部分内容,至于项目所需要的jar包,网上多的是...不列出了!



package net.zdsoft.eis.controller;

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

@Controller
@RequestMapping("/test2/login.do")  // 指定唯一一个*.do请求关联到该Controller
public class TestController2 {
    
    @RequestMapping
    public String testLogin(String username, String password, int age) {
        // 如果不加任何参数,则在请求/test2/login.do时,便默认执行该方法
        
        if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return "loginError";
        }
        return "loginSuccess";
    }

    @RequestMapping(params = "method=1", method=RequestMethod.POST)
    public String testLogin2(String username, String password) {
        // 依据params的参数method的值来区分不同的调用方法
        
        // 可以指定页面请求方式的类型,默认为get请求
        System.out.println(username);
        if (!"admin".equals(username) || !"admin".equals(password)) {
            return "loginError";
        }
        return "loginSuccess";
    }
    
    @RequestMapping(params = "method=2")
    public String testLogin3(String username, String password, int age) {
    	// eg: http://localhost:8080/test2/login.do?username=admin&password=admin&age=6&method=2
    	if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
            return "loginError";
        }
        System.out.println(username);
        return "loginSuccess";
    }
}


 

 

 


package com.css.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.css.model.Emp;


@Repository("empDAO")
public class EmpDAO {
	@Resource
	private HibernateTemplate hibernateTemplate;
	
	

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	
	
	private static final Logger log = LoggerFactory.getLogger(EmpDAO.class);
	// property constants
	public static final String ENAME = "ename";
	public static final String JOB = "job";
	public static final String MGR = "mgr";
	public static final String SAL = "sal";
	public static final String COMM = "comm";
	public static final String DEPTNO = "deptno";

	protected void initDao() {
		// do nothing
	}

	public void save(Emp transientInstance) {
		log.debug("saving Emp instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Emp persistentInstance) {
		log.debug("deleting Emp instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Emp findById(java.lang.Short id) {
		log.debug("getting Emp instance with id: " + id);
		try {
			Emp instance = (Emp) getHibernateTemplate().get("com.css.dao.Emp",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Emp instance) {
		log.debug("finding Emp instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Emp instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Emp as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByEname(Object ename) {
		return findByProperty(ENAME, ename);
	}

	public List findByJob(Object job) {
		return findByProperty(JOB, job);
	}

	public List findByMgr(Object mgr) {
		return findByProperty(MGR, mgr);
	}

	public List findBySal(Object sal) {
		return findByProperty(SAL, sal);
	}

	public List findByComm(Object comm) {
		return findByProperty(COMM, comm);
	}

	public List findByDeptno(Object deptno) {
		return findByProperty(DEPTNO, deptno);
	}

	public List findAll() {
		log.debug("finding all Emp instances");
		try {
			String queryString = "from Emp";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Emp merge(Emp detachedInstance) {
		log.debug("merging Emp instance");
		try {
			Emp result = (Emp) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Emp instance) {
		log.debug("attaching dirty Emp instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Emp instance) {
		log.debug("attaching clean Emp instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static EmpDAO getFromApplicationContext(ApplicationContext ctx) {
		return (EmpDAO) ctx.getBean("EmpDAO");
	}
}


 


<?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">
	<context:component-scan base-package="com.css.*" />
	<context:property-placeholder location="classpath:/hibernate.properties" />
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName">
			<value>${dataSource.driverClassName}</value>
		</property>
		<property name="username">
			<value>${dataSource.username} </value>
		</property>
		<property name="password">
			<value>${dataSource.password} </value>
		</property>
		<property name="url">
			<value>${dataSource.url}</value>
		</property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:hibernate.cfg.xml" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${dataSource.dialect}</prop>
				<prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop>
				<prop key="hibernate.show_sql">${dataSource.show_sql}</prop>
			</props>
		</property>
	</bean>
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!--配置一个JdbcTemplate实例 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
</beans>


 


uploadDir=upload


 


dataSource.dialect=org.hibernate.dialect.OracleDialect
dataSource.driverClassName=oracle.jdbc.driver.OracleDriver
dataSource.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
dataSource.username=springmvc
dataSource.password=springmvc
dataSource.hbm2ddl.auto=update
dataSource.show_sql=true


 

 

在Spring MVC项目中,配置前端控制器是非常重要的一步。可以通过在web.xml文件中配置前端控制器,对浏览器发送的请求进行统一处理。\[1\]在配置文件中,需要指定前端控制器的servlet-name和servlet-class,以及加载Spring配置文件的位置和名称。可以使用init-param来指定contextConfigLocation,这个参数的值是指定Spring配置文件的路径,一般是在classpath下的springmvc.xml文件。\[1\] 在控制器类中,使用@Controller注解来标识这是一个控制器类。可以使用@RequestMapping注解来指定请求的URL路径。在控制器方法中,可以调用相应的服务类来处理业务逻辑,并将结果存储在ModelAndView对象中,然后返回给视图进行展示。\[2\] 在配置文件springmvc.xml中,可以进行一些其他的配置,比如设置视图解析器、配置拦截器、配置静态资源等。\[3\] 总结起来,配置Spring MVC项目需要在web.xml中配置前端控制器,指定Spring配置文件的位置和名称,同时在控制器类中使用注解来标识和处理请求。在springmvc.xml配置文件中可以进行其他的配置。 #### 引用[.reference_title] - *1* *3* [SpringMVC配置](https://blog.csdn.net/qq_61353850/article/details/124329884)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringMvc项目完整配置](https://blog.csdn.net/u010963948/article/details/40044717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值