zoukankan      html  css  js  c++  java
  • web框架之Spring-MVC环境搭建

    spring框架jar包

    1、下载spring源包
    spring地址:http://www.springsource.org/download
    我下的是spring-framework-3.1.0.RELEASE-with-docs.zip
    下载依赖包:spring-framework-3.0.5.RELEASE-dependencies.zip
    注意官网上3.0.3版本以后同版本依赖包不提供下载

    2、导入所需jar包
    引入dist目录下除了下面三个其余所有包
    org.springframework.web.struts-3.1.0.RELEASE.jar
    org.springframework.spring-library-3.1.0.RELEASE.libd
    org.springframework.web.portlet-3.1.0.RELEASE.jar
    引入依赖包下com.springsource.org.apache.commons.logging-1.1.1.jar及com.springsource.org.aopalliance-1.0.0.jar

    spring框架配置

    1、web.xml配置

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app   
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns="http://java.sun.com/xml/ns/javaee"   
    5.     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    6.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    7.     id="WebApp_ID"   
    8.     version="3.0">  
    9.     <context-param>  
    10.         <param-name>contextConfigLocation</param-name>  
    11.         <!-- 应用上下文配置文件 -->  
    12.         <param-value>/WEB-INF/spring-servlet.xml</param-value>  
    13.     </context-param>  
    14.     <listener>  
    15.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    16.     </listener>  
    17.     <!-- 配置spring核心servlet -->  
    18.     <servlet>  
    19.         <servlet-name>spring</servlet-name>  
    20.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    21.         <load-on-startup>1</load-on-startup>  
    22.     </servlet>  
    23.     <!-- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->  
    24.     <servlet-mapping>  
    25.         <servlet-name>spring</servlet-name>  
    26.         <url-pattern>/</url-pattern>  
    27.     </servlet-mapping>  
    28. </web-app>  

    2、应用上下文配置
    spring-servlet.xml即配置用于开启基于注解的springMVC功能,照web.xml中设定,路径为WEB-INF下

    [html] view plain copy
     
    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.  xmlns:context="http://www.springframework.org/schema/context"  
    3.  xmlns:p="http://www.springframework.org/schema/p"  
    4.  xmlns:mvc="http://www.springframework.org/schema/mvc"  
    5.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    6.  xsi:schemaLocation="http://www.springframework.org/schema/beans  
    7.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    8.       http://www.springframework.org/schema/context  
    9.       http://www.springframework.org/schema/context/spring-context.xsd  
    10.       http://www.springframework.org/schema/mvc  
    11.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
    12.      <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->  
    13.      <mvc:annotation-driven />  
    14.      <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->  
    15.      <context:component-scan base-package="com.mvc.rest" />  
    16.      <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->  
    17.      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" />  
    18. </beans>  

    Demo例子
    1、根据spring-servlet.xml配置的包路径(com.mvc.rest)新建Constroller

    [java] view plain copy
     
    1. package com.mvc.rest;  
    2. import javax.servlet.http.HttpServletRequest;  
    3. import javax.servlet.http.HttpServletResponse;  
    4. import org.springframework.stereotype.Controller;  
    5. import org.springframework.ui.ModelMap;  
    6. import org.springframework.web.bind.annotation.PathVariable;  
    7. import org.springframework.web.bind.annotation.RequestMapping;  
    8. import org.springframework.web.bind.annotation.RequestMethod;  
    9. import org.springframework.web.servlet.ModelAndView;  
    10. @Controller  
    11. public class RestConstroller {  
    12.     public RestConstroller() {}  
    13.     @RequestMapping(value = "/login/{user}", method = RequestMethod.GET)  
    14.     public ModelAndView myMethod(HttpServletRequest request,HttpServletResponse response,   
    15.             @PathVariable("user") String user, ModelMap modelMap) throws Exception {  
    16.         modelMap.put("loginUser", user);  
    17.         return new ModelAndView("/login/hello", modelMap);  
    18.     }  
    19.     @RequestMapping(value = "/welcome", method = RequestMethod.GET)  
    20.     public String registPost() {  
    21.         return "/welcome";  
    22.     }  
    23. }  

    2、建jsp视图

    视图路径在spring-servlet.xml配置(/WEB-INF/view/),据上述RestConstroller 类,我们在WEB-INF下建立view目录,在view下建立welcome.jsp及login/hello.jsp
    welcome.jsp随意,hello.jsp代码如下:

    [html] view plain copy
     
    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    2. <%  
    3. String path = request.getContextPath();  
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    5. %>  
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    7. <html>  
    8. <head>  
    9. <base href="<%=basePath%>">  
    10. <title>My JSP 'hello.jsp' starting page</title>  
    11. <meta http-equiv="pragma" content="no-cache">  
    12. <meta http-equiv="cache-control" content="no-cache">  
    13. </head>  
    14. <body>  
    15.     你好:<%=request.getAttribute("loginUser") %>,现在时间是<%= new Date() %>  
    16. </body>  
    17. </html>  

    3、部署访问
    http://localhost:8080/SpringMvcDemo/welcome

  • 相关阅读:
    关于celery踩坑
    关于git的分批提交pull requests流程
    SymGAN—Exploiting Images for Video Recognition: Heterogeneous Feature Augmentation via Symmetric Adversarial Learning学习笔记
    AFN—Larger Norm More Transferable: An Adaptive Feature Norm Approach for Unsupervised Domain Adaptation学习笔记
    Learning to Transfer Examples for Partial Domain Adaptation学习笔记
    Partial Adversarial Domain Adaptation学习笔记
    Partial Transfer Learning with Selective Adversarial Networks学习笔记
    Importance Weighted Adversarial Nets for Partial Domain Adaptation学习笔记
    Exploiting Images for Video Recognition with Hierarchical Generative Adversarial Networks学习笔记
    improved open set domain adaptation with backpropagation 学习笔记
  • 原文地址:https://www.cnblogs.com/zhaoyan001/p/7513759.html
Copyright © 2011-2022 走看看