Spring MVC 框架搭建设置要领及详解

此刻主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名措施员需要把握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了。不外要想机动运用Spring MVC来应对大大都的Web开拓,就必需要把握它的设置及道理。

一、Spring MVC情况搭建:(Spring 2.5.6 + Hibernate 3.2.0)

1. jar包引入

  Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar

  Hibernate 3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相应数据库的驱动jar包

2. web.xml设置(部门)

<!-- Spring MVC设置 --> <!-- ====================================== --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自界说servlet.xml设置文件的位置和名称,默认为WEB-INF目次下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> 默认 </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Spring设置 --> <!-- ====================================== --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring Bean的设置文件地址目次。默认设置在WEB-INF目次下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param>

3. spring-servlet.xml设置

  spring-servlet这个名字是因为上面web.xml中<servlet-name>标签配的值为spring(<servlet-name>spring</servlet-name>),再加上“-servlet”后缀而形成的spring-servlet.xml文件名,假如改为springMVC,对应的文件名则为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: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/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx/spring-tx-3.0.xsd <A href="https://www.springframework.org/schema/context/spring-context-3.0.xsd"></A>"> <!-- 启用spring mvc 注解 --> <context:annotation-config /> <!-- 配置利用注解的类地址的jar包 --> <context:component-scan base-package="controller"></context:component-scan> <!-- 完成请求和注解POJO的映射 --> <bean /> <!-- 对转向页面的路径理会。prefix:前缀, suffix:后缀 --> <bean p:prefix="/jsp/" p:suffix=".jsp" /> </beans>

4. 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" xsi:schemaLocation=" http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 回收hibernate.cfg.xml方法设置数据源 --> <bean> <property> <value>classpath:config/hibernate.cfg.xml</value> </property> </bean> <!-- 将事务与Hibernate关联 --> <bean> <property> <ref local="sessionFactory"/> </property> </bean> <!-- 事务(注解 )--> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 测试Service --> <bean></bean> <!-- 测试Dao --> <bean> <property ref="sessionFactory"></property> </bean> </beans>

二、详解

  Spring MVC与Struts从道理上很相似(都是基于MVC架构),都有一个节制页面请求的Servlet,处理惩罚完后跳转页面。看如下代码(注解):

package controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import entity.User; @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"; } }

以上4个要领示例,是一个Controller里含有差异的请求url,也可以回收一个url会见,通过url参数来区分会见差异的要领,代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsddxz.html