springmvc中的spring-servlet.xml 和 applicationContext.xml

首先说这两个配置文件的区别:
  引用地址: 点击打开链接
因为直接使用了SpringMVC,所以之前一直不明白xxx-servlet.xml和applicationContext.xml是如何区别的,其实如果直接使用SpringMVC是可以不 添加applicationContext.xml文件的。
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
而这个一般是采用非spring mvc架构,如使用struts之类而又想引入spring才添加的,这个是用来加载Application Context。

如果直接采用SpringMVC,只需要把所有相关配置放到xxx-servlet.xml中就OK了。


两个配置文件的详解:

引用地址: 点击打开链接

在我们进行 spring-servlet 进行开发的时候,经常会遇到配置文件配置的问题,要彻底的解决这个问题,我们需要了解 springMVC 设计的基本架构

1.SpringMVC 的配置分为两部分 application.xml 和 spring-servlet.xml

2.两个配置文件的作用和配置位置

2.1.application.xml :对应的是系统级别的配置,作用范围是系统上下文。

2.2.spring-servlet.xml:对应的是 controller 级别的配置,作用范围是控制层上下文。

3.它们在web.xml 中的配置

3.1.因为 application.xml 是系统级别的上下文,所以它的初始化需要放到 web.xml 中的<context-param>标签中,同时其他的类似定时任务的配置文件等等都是放在这个标签下进行初始化的。

3.2.因为spring-servlet.xml只是 controller 级别的上下文,说白了就是 servlet 级别的初始化,它不涉及到除了转发之外的任何实体,所以它的作用范围仅仅限制在 servlet 级别,所以它的初始化应该是跟spring 的 DispatcherServlet 初始化在一起,所以就是在 <servlet> 表情中初始化的。它有一个默认值就是【/WEB-INF/remoting-servlet.xml 】,注意配置文件的对应的名称是【 servlet-name】-servlet.xml,所以如果你没有给servlet 制定配置文件的位置,并且在默认位置下也没有配置文件,那么系统启动的时候就会报错。


注意:对于 servlet配置文件里面应该初始化的东西,除了视图的解析方式、静态资源文件的存放位置、controller的初始化方式之外,其他的都不应该放在 servlet 配置文件中,应为它只负责 请求的转发,返回结果的解析以及静态资源文件的解析,其他的对象的初始化,定时任务...都不应该放到这个配置文件下进行管理。


[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  4.                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     version="3.0" metadata-complete="true">  
  6.       
  7.     <!--  这个地方默认加载的是系统的变量的配置文件,它们属于是系统级别的配置  -->  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>  
  11.         classpath:spring/application.xml.xml,  
  12.         classpath:spring/spring-quartz.xml  
  13.         </param-value>  
  14.     </context-param>  
  15.     <context-param>  
  16.         <param-name>webAppRootKey</param-name>  
  17.         <param-value>webapp.root</param-value>  
  18.     </context-param>  
  19.     <listener>  
  20.         <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>  
  21.     </listener>  
  22. <!--     <context-param>  
  23.         <param-name>logbackConfigLocation</param-name>  
  24.         <param-value>classpath:conf/logback.xml</param-value>  
  25.     </context-param> -->  
  26. <!--     <listener>  
  27.         <listener-class>xorg.springframework.web.util.LogbackConfigListener</listener-class>  
  28.     </listener> -->  
  29.     <listener>  
  30.         <listener-class>com.cloudFarmHDAPI.admin.listener.SystemListener</listener-class>  
  31.     </listener>  
  32.       
  33.     <!--  这个地方加载的是 servlet 的变量的配置文件,它们属于 controller 级别的配置  
  34.         1.如果不配置这个 servlet-context.xml 的配置文件位置,  
  35.         那么默认就会去/WEB-INF/servlet-context.xml 下面去寻找这个文件   
  36.         2.如果配置了这个位置,那么它就会去制定位置加载文件  
  37.       -->  
  38.     <servlet>  
  39.         <servlet-name>appServlet</servlet-name>  
  40.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  41.         <init-param>  
  42.             <param-name>contextConfigLocation</param-name>  
  43.             <param-value>classpath:spring/servlet-context.xml</param-value>  
  44.         </init-param>  
  45.         <load-on-startup>1</load-on-startup>  
  46.     </servlet>  
  47.     <servlet-mapping>  
  48.         <servlet-name>appServlet</servlet-name>  
  49.         <url-pattern>/</url-pattern>  
  50.     </servlet-mapping>  
  51.     <!-- charactor encoding -->  
  52.     <filter>  
  53.         <filter-name>encodingFilter</filter-name>  
  54.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  55.         <init-param>  
  56.             <param-name>encoding</param-name>  
  57.             <param-value>utf-8</param-value>  
  58.         </init-param>  
  59.     </filter>  
  60.     <filter-mapping>  
  61.         <filter-name>encodingFilter</filter-name>  
  62.         <url-pattern>/*</url-pattern>  
  63.     </filter-mapping>  
  64.     <!-- shiro security filter -->  
  65.     <filter>  
  66.         <filter-name>shiroSecurityFilter</filter-name>  
  67.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  68.         <init-param>  
  69.             <param-name>targetFilterLifecycle</param-name>  
  70.             <param-value>true</param-value>  
  71.         </init-param>  
  72.     </filter>  
  73.     <filter-mapping>  
  74.         <filter-name>shiroSecurityFilter</filter-name>  
  75.         <url-pattern>/*</url-pattern>  
  76.         <dispatcher>REQUEST</dispatcher>  
  77.         <dispatcher>FORWARD</dispatcher>  
  78.         <dispatcher>ERROR</dispatcher>  
  79.     </filter-mapping>  
  80.     <session-config>    
  81.       <session-timeout>60</session-timeout>    
  82.     </session-config>   
  83.     <welcome-file-list>    
  84.         <welcome-file>index.htm</welcome-file>    
  85.     </welcome-file-list>  
  86. </web-app>  

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现上述功能的步骤: 1. 创建一个SpringMVC项目,可以使用Spring Tool Suite等IDE工具来创建。 2. 在webapp目录下创建login.jsp页面,该页面包含一个表单,可以输入用户名和密码,并且有一个提交按钮。 3. 在web.xml文件配置DispatchServlet,将其映射到“/”路径下。 4. 在Spring-mvc.xml(applicationContext.xml)文件配置Controller和视图解析器。 5. 编写Controller类,其包含userlogin方法,该方法接收用户输入的用户名和密码,并进行相应的处理。 6. 在userlogin方法,可以使用HttpServletRequest对象来获取用户输入的用户名和密码。 7. 如果用户名和密码是ZhangSan和123时,则跳转到main.jsp页面,并显示用户名在页面上。否则,跳转到login.jsp页面,并显示用户名或密码错误。 8. 在main.jsp页面,显示用户输入的用户名。 下面是实现上述功能的代码示例: 1. login.jsp页面 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login Page</title> </head> <body> <h2>Login Page</h2> <form action="${pageContext.request.contextPath}/userlogin" method="post"> <label for="userName">UserName:</label> <input type="text" id="userName" name="userName"/><br/><br/> <label for="password">Password:</label> <input type="password" id="password" name="password"/><br/><br/> <input type="submit" value="Login"/> </form> </body> </html> ``` 2. 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/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.example.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans> ``` 3. Controller类 ``` package com.example.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class UserController { @RequestMapping(value = "/login", method = RequestMethod.GET) public String showLoginPage() { return "login"; } @RequestMapping(value = "/userlogin", method = RequestMethod.POST) public String userLogin(HttpServletRequest request, Model model) { String userName = request.getParameter("userName"); String password = request.getParameter("password"); if (userName.equals("ZhangSan") && password.equals("123")) { model.addAttribute("userName", userName); return "main"; } else { model.addAttribute("errorMsg", "Username or password is incorrect"); return "login"; } } } ``` 4. main.jsp页面 ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Main Page</title> </head> <body> <h2>Welcome ${userName}!</h2> </body> </html> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值