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

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

最近公司用到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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 郑州出院限号怎么办 林场改革森林消防怎么办 网络课没有看完怎么办 福建省会计证未换怎么办 国税地税登记后怎么办 网上发票领不了怎么办 合肥餐饮怎么办环评 地税财务报表上传错误怎么办? 平安体检卡过期怎么办 到医院挂号后怎么办 一吹风就头痛怎么办 平安口袋e怎么办离职 学员学分卡号怎么办 华医网注册错了怎么办 医师继续教育学分不够怎么办 执业医师学分卡怎么办 护士学分5年没办怎么办 华医网职称录入错误怎么办 在北京没有学历怎么办 继续教育证书丢了怎么办 继续教育证丢了怎么办 继续教育档案丢了怎么办 现在会计证丢了怎么办 初级准考证丢了怎么办 协助调查48小时怎么办 有个抑郁症父亲怎么办 法院不给立案怎么办 眼睛发炎有眼屎怎么办 外伤后眼白出血怎么办 斜视手术失败了怎么办 角膜划伤恢复慢怎么办 机场海关扣行李怎么办 被消防参谋恐吓怎么办 公安局贴的封条怎么办 公司倒闭后诉讼怎么办 股东不配合清算怎么办 消防罚款不交怎么办 武警撤勤海关怎么办 包包掉出租车上怎么办 打市长热线不管怎么办 科目三不好预约怎么办