Spring+Spring MVC+Hibernate环境搭配
Spring+Spring MVC+Hibernate简称"SSH"。Spring容器是Spring的核心,该 容器负责管理spring中的java组件。Spring的核心机制:依赖注入。Hibernate是一个不错的ORM(关系对象映射)框架。Spring+Spring MVC+Hibernate环境搭配步骤:
1、搭建Spring+Hibernate环境(跟ssh搭建步骤一致)
(1)加入Spring+Hibernate的架包。
2、搭建SpringMVC环境
( 1)添加Springmvc依赖包:spring-webmvc.jar
(2)配置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"> <!-- spring监听器: --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定spring文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- =====================配置spring mvc Start================================= --> <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:springmvc.xml</param-value> </init-param> <!-- >=0代表web容器启动的时候加载servlet(数字代表优先级) --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
(3)配置springmvc.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:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- SpringMVC的配置规则和Spring是一样的:无缝集成 --> <!-- 配置视图 --> <bean id="internalView" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置视图前缀 --> <!-- <property name="prefix" value="WEB-INF/jsp/" />--> <property name="prefix" value="/" /> <!-- 配置视图后缀 --> <property name="suffix" value=".jsp" /> </bean> <!-- 配置URL映射 --> <bean id="urlMpping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="userController.do">userController</prop> <prop key="perpoController.do">perpoController</prop> <prop key="multiMethodController.do">multiMethodController</prop> </props> </property> </bean> <!-- 多方法调用规则 --> <bean id="parameterMethod" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <!-- value随意;但是还是要有一定意义 --> <property name="paramName" value="method" /> </bean> <!-- 将控制器交由Spring容器管理 --> <bean id="userController" class="com.ssh.SpringMVC.controller.UserController"></bean> <bean id="perpoController" class="com.ssh.SpringMVC.controller.PerpoController"></bean> <!-- 一个controllet中配置多个方法 --> <bean id="multiMethodController" class="com.ssh.SpringMVC.controller.MultiMenthodController"> <!-- 注入多方法调用规则 --> <property name="methodNameResolver" ref="parameterMethod"></property> </bean> </beans>
登入页面:
<%@ 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 'MyJsp.jsp' starting page</title> </head> <body> <form action="multiMethodController.do?method=update" method="post"> <input type="text" name="username" /> <input type="submit" /> </form> </body> </html>
表单提交处理MultiMenthodController.java文件
package com.msit.springmvc.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class MultiMenthodController extends MultiActionController { /** * url:http://localhost:8080/SpringMVCDay1/multiMethodController.do?method=login * @return */ public ModelAndView login(HttpServletRequest request,HttpServletResponse response) { System.out.println("这里是login方法"+request.getParameter("username")); return new ModelAndView("MyJsp"); } public ModelAndView update(HttpServletRequest request,HttpServletResponse response) { System.out.println("这里是update方法"); return new ModelAndView("MyJsp"); } }
效果图:
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态。 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内容有任何疑问, 可以通过评论或发邮件的方式联系我: 2276292708@qq.com 如果需要转载,请注明出处,谢谢!!
责任重于泰山,任重而道远。