zoukankan      html  css  js  c++  java
  • SpringMVC入门示例

    1、新建一个Java Web项目

    2、导入jar包

    3、在WEB-INF下面建一个hello.jsp页面。

    复制代码
     1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
     2 
     3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     4 <html>
     5   <head>
     6     <title>My JSP 'hello.jsp' starting page</title>
     7   </head>
     8   
     9   <body>
    10     hello springmvc
    11   </body>
    12 </html>
    复制代码

    4、配置web.xml文件

    复制代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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_3_0.xsd" id="WebApp_ID" version="3.0">
     3 
     4     <servlet>
     5         <servlet-name>dispatcherServlet</servlet-name>
     6         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     7         <init-param>
     8             <param-name>contextConfigLocation</param-name>
     9             <param-value>classpath:spring-mvc.xml</param-value>
    10         </init-param>
    11         <load-on-startup>1</load-on-startup>
    12     </servlet>
    13     
    14     <servlet-mapping>
    15         <servlet-name>dispatcherServlet</servlet-name>
    16         <url-pattern>/</url-pattern>
    17     </servlet-mapping>
    18     
    19     <welcome-file-list>
    20         <welcome-file>index.jsp</welcome-file>
    21     </welcome-file-list>
    22 </web-app>
    复制代码

    5、配置spring-mvc.xml文件

    复制代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
     6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
     7 
     8     <context:component-scan base-package="com.proc"></context:component-scan>
     9 
    10     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    11         <property name="prefix" value="/WEB-INF/"/>
    12         <property name="suffix" value=".jsp"></property>
    13     </bean>
    14 </beans>
    复制代码

      InternalResourceViewResolver:视图解析器。根据Url地址遭到找到文件资源

        prefix:前缀

        suffix:后缀

    6、配置控制器

    复制代码
     1 package com.proc;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 @Controller
     7 public class Hello {
     8 
     9     @RequestMapping("/hello")
    10     public String hello(){
    11         System.out.println("hello springmvc");
    12         return "hello";
    13     }
    14 }
    复制代码

      @RequetMapping注解作用是url映射

    测试访问

    本文转自:http://www.cnblogs.com/caoyc/p/5635101.html

  • 相关阅读:
    Putty远程登录VMware虚拟机Linux(Ubuntu12.04)
    boost库在工作(39)网络UDP异步服务端之九
    UVA 1401 Remember the Word
    Windbg调试命令详解(1)
    数学之路(3)-机器学习(3)-机器学习算法-余弦相似度(1)
    2012-2013年度总结
    重建二叉树---根据前序和中序遍历结果重建二叉树
    Windbg调试命令详解(2)
    时间操作(JavaScript版)—最简单比較两个时间格式数据的大小
    WO+开放平台:API调用开发手记(话费计费接口2.0)
  • 原文地址:https://www.cnblogs.com/jasonZh/p/8760908.html
Copyright © 2011-2022 走看看