springmvc——第一篇入门

1.构建web的maven工程(具体看idea的web构建篇:
idea的web构建篇
):
在这里插入图片描述
2.直接建立类,一部到位:
在这里插入图片描述
在这里插入图片描述
2.一个简单的springmvc例子:
在这里插入图片描述
HelloContorller.java

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String sayHello(){
        System.out.println("hello StringMvc");

        return "success";
    }

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!--1.开启注解扫描,相当于把它交给ispringoc容器-->
<context:component-scan base-package="cn.xiaosi.controller"></context:component-scan>
<!--2.配置视图解析器解析类
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String sayHello(){
        System.out.println("hello StringMvc");
        return "success";
    }
-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--告诉我页面路径在哪里-->
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <!--文件是以什么结尾的-->
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--3.开启动springmvc框架注解支持-->
    <mvc:annotation-driven/>
</beans>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--一启动就加载配置文件启动sprigmvc.xml的配置文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    <!--这里是拦截所有的路径-->
  </servlet-mapping>
</web-app>



index.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3 >sprintmvc入门</h3>
    <a href="hello">aaaa</a>
</body>
</html>

success.html


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
成功了
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值