zoukankan      html  css  js  c++  java
  • springmvc复习笔记----springmvc最简单的第一个例子:RequestMapping试水

    结构


    用到的包


    web.xml

    <url-pattern>/</url-pattern>中可以换成其他的后缀*.do ,*. sb  ……

     <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>springmvc01</display-name>
      <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

    spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
            xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!--   自动扫描加载注解的包 -->
    <context:component-scan base-package="com.ij34.bean"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/view/"></property>
    <property name="suffix" value=".jsp" ></property>
    </bean>
    </beans>

     helloworld.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ${message}
    </body>
    </html>

    index.jsp

    如果上面的web.xml中

    <url-pattern>/</url-pattern>中可以换成其他的后缀*.do ,*. sb  ……

     下面的href里也要换成相应的后缀

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <a href="hello">访问springMVC的helloworld</a>
    </body>
    </html>

    Testhello.java

    package com.ij34.bean;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class Testhello {
        @RequestMapping("/hello")      //对应于index.jsp的hello请求
     public String hello(Model model) {
         model.addAttribute("message", "我是springmvc");
        return "helloworld";                      //返回到view的helloworld.jsp
        // TODO Auto-generated method stub
    
    }
    }

  • 相关阅读:
    ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
    MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction
    二叉树的建立&&前中后遍历(递归实现)&&层次遍历
    实现一个简单的散列表(HashMap)
    单向链表的删除及插入操作(以头插入法建立单向链表)
    单向链表的建立(头插入法)
    单向链表的建立(尾部插入法)
    链式队列(单向列表实现)
    顺序队列(数组实现)
    链式栈(单向链表实现)
  • 原文地址:https://www.cnblogs.com/tk55/p/6652394.html
Copyright © 2011-2022 走看看