一步步搭建Springmvc+hibernate框架(1)——spring mvc

来源:互联网 发布:淘宝卖自制化妆品 编辑:程序博客网 时间:2024/06/16 00:58

最近公司用到hibernate,自己好多年没用hibernate了,所以现在试试。

这次建立的是maven项目,项目已经建好了,如何创建,这里我不在多说,不会的亲参考我其他文章。

首先第一步,先引入spring mvc框架:

导入相关jar包。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mx</groupId>
  <artifactId>ssh</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>ssh Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
<springframework.version>4.0.6.RELEASE</springframework.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

<!--springmvc包  -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>

<dependency><groupId>org.springframework</groupId><artifactId>spring-conext</artifactId><version>${springframework.version}</version></dependency>

<!--servlet包,用户处理HttpServletRequest等请求,参数等问题  -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
  </dependencies>
  <build>
    <finalName>ssh</finalName>
  </build>
</project>

然后新建spring请求处理文件

spring.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
        
        
        <!-- 配置自动扫描的包 -->
        <context:component-scan base-package="com.mx"></context:component-scan>
        
        <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/WEB-INF/views/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>
</beans>

接着在web.xml <web-app>中添加spring拦截器springDispatcherServlet

<servlet>
  <servlet-name>springDispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 配置Spring mvc下的配置文件的位置和名称 -->
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>springDispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>

然后根据在前面spring.xml中配置的<property name = "prefix" value="/WEB-INF/views/"></property>

在/WEB-INF/文件夹下创建views文件夹,以后跳转后的界面就放在此处,spring跳转后也会转到此处寻找跳转后的页面

好了,下面正式测试:

新建一个controller



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


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




@Controller
@RequestMapping("/index")
public class IndexController {

@RequestMapping("/test")
public String index(HttpServletRequest request,HttpServletResponse response){


return "index";
}
}

然后在/WEB-INF/views/文件夹下创建index.jsp页面

启动项目:maven项目,先在项目右键执行Run——》Maven claen

再执行Run->Maven install

如果没有报错。

,再把项目部署到tomcat,启动tomcat。后在浏览器输入http://localhost/ssh/index/test



阅读全文
1 0