SpringMVC的环境搭建

SpringMVC的环境搭建

SpringMVC的原理图:

springMVC是基于Spring的  所以要导入spring的包:
aopalliance
commons-loggging
spring-aop
spring-beans
spring-context
spring-core
spring-expression

要用springMVC要引入
spring-web
spring-webmvc


用到jsp  jstl就要假如jstl的包和serlvet的
引入
javax.servlet-api-3.0.1
jstl-1.2

最终要引入的所有包:
记得要手动把包引入web项目!!

1.在resource包右键new  XML Configuration File 选SpringConfig  创建spring-mvc.xml
还要更改XML约束为
<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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

spring-mvc.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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--开启springmvc的注解驱动-->
    <mvc:annotation-driven/>
    <!--配置包扫描-->
    <context:component-scan base-package="com.dengweiquan.**" />

    <!--配置viewResolver-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--getrequestdispature的时候要填的前缀  和后准都自动填好-->
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
        <!--jsp要用到jstl-->
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
     </bean>
</beans>

2.在web.xml里面配置DispatherServlet
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
          <!--配置DispatcherServlet-->  
<servlet>
    <servlet-name>ssm-demo</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--声明spring-mvc配置所在位置-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:./com/dengweiquan/resource/spring-mvc.xml</param-value>
    </init-param>
    <!--设置工程启动之后就立马加载这个servlet  设置为优先级最高-->
    <load-on-startup>1</load-on-startup>
</servlet>
    <servlet-mapping>
        <servlet-name>ssm-demo</servlet-name>
        <!--斜杠拦截所有请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

3.创建实体类User:
public class User {

    private int id;
    private String name;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

4.创建UseConroller类

//打了@Controller  都归springMVC管
//打了@Controller  是写请求路径的   假如请求路径里面是带user的   例如user/test.jsp  或者user/demo.jsp   这个请求就会分发到这个controller来处理
@Controller
@RequestMapping("user")
public class UserController {
    @RequestMapping("/test")
    public String testUser(){

        //返回userspringmvc自动帮你加好了前后缀  就是我们要去的user.jsp   在viewResolver哪里已经配置好了路径的前后缀
        return "user";
    }
    @RequestMapping("/demo")
    public String demoUser(){


        //返回userspringmvc自动帮你加好了前后缀  就是我们要去的user.jsp   在viewResolver哪里已经配置好了路径的前后缀
        return "demo";
    }
   
}

5.在WEB-INF目录下创建view目录  再创建demo.jsp   和user.jsp
demo.jsp :
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
这里是demo.jsp
</body>
</html>

user.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
这里是user.jsp
</body>
</html>

项目目录结构:

启动项目  然后输入网址  http://localhost:8080/user/test   页面会显示这里是user.jsp 
demo也同理
SpringMVC搭建完毕  可喜可贺O(∩_∩)O












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值