1.可以自动加载注解驱动,通过注解找到对应Controller

    <!-- spring MVC 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置自动扫描包 -->
<context:component-scan base-package="com.cc8w.Controller"></context:component-scan> <!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
package com.cc8w.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/HelloWorld")
public class HelloWorld { @RequestMapping("/hi.do")
public String hi(String names,Model model)
{
System.out.println("hi.dodo");
model.addAttribute("hi", "hi123456");
return "hi";
}
}

显示结果正确.

2.配置HandlerMapping,根据beanName找到对应Controller

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Spring-web02</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<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:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置HandlerMapping 根据beanname找到对应Controller -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"></bean> <!--配置beanname找到对应的Controller -->
<bean name = "/dogcontrollerdog" class = "com.cc8w.Controller.Dog"></bean> <!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

测试类(这里一定要继承org.springframework.web.servlet.mvc.AbstractController接口)

package com.cc8w.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; public class Dog extends AbstractController {
public Dog()
{
System.out.println("13213");
} @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
System.out.println("456--访问到了---"); ModelAndView mav = new ModelAndView("dog");
mav.addObject("hi", "467 th");
return mav;
}
}

模版目录和结果

3.配置HandlerMapping,根据URL找到对应Controller(主要是applicationContext.xml或<bean-name>-Servlet.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置HandlerMapping 根据简单URL找到对应Controller -->
<bean class = "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/dogcontroller">dog</prop>
</props>
</property>
</bean>
<!--bean类 -->
<bean id = "dog" class = "com.cc8w.Controller.Dog"></bean> <!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

上面的类,这个spring MVC配置文件,可以找到控制器,就不截 结果了.

4.配置HandlerMapping,通过控制器类名访问Controller,访问时类名首字母小写 小写

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置HandlerMapping 通过控制器类名访问Controller,访问时类名首字母小写 -->
<bean class = "org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean> <!--bean类 -->
<bean class = "com.cc8w.Controller.Dog"></bean> <!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean> </beans>

http://localhost:8080/Spring-web02/dog 这样访问...

注意, 还呀配置下日志.

https://www.cnblogs.com/fps2tao/p/12809867.html

Spring MVC 的xml一些配置的更多相关文章

  1. Spring MVC 的 XML 配置方式

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml solution/webapi/pom.xml solution/mapper/ ...

  2. Spring MVC Web.xml配置

    Web.xml spring&spring mvc 在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个 ...

  3. Spring MVC 返回 xml json pdf 数据的配置方法

    <!-- Spring MVC 返回 xml 数据的配置方法 -->     <bean class="org.springframework.web.servlet.vi ...

  4. Spring MVC 使用tomcat中配置的数据源

    Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources&g ...

  5. Spring mvc web.xml中 urlpatten的配置问题

    在使用spring mvc 是我们会配置spring 的DispatcherServlet作为请求的转发器. <servlet> <servlet-name>spring< ...

  6. Spring配置文件beans.xml头部配置解释

    Spring配置文件beans.xml头部配置解释 - EasonJim - 博客园https://www.cnblogs.com/EasonJim/p/6880329.html

  7. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  8. Spring MVC 3.x 基本配置

    WEB-INF/web.xml 例1 <?xml version="1.0" encoding="UTF-8"?> <web-app xmln ...

  9. 使用高性能xml序列化框架jibx作为spring mvc的xml view

    package org.springframework.web.servlet.view.xml; import java.io.ByteArrayOutputStream; import java. ...

随机推荐

  1. Windows下创建文件的权限问题

    在Windows下如果在某个目录下建立一个文件,那么新建立的文件会默认继承该目录的所有权限(父子关系) 如果将一个文件从一个目录移动到到另一个目录下,那么该文件的权限并不会继承自新目录的权限而是还保留 ...

  2. C#.NET常见问题(FAQ)-TabControl如何隐藏和显示页面

    如果需要显示某个页面,则让他的Parent就是TabControl的控件名称,如果要隐藏,则等于null      private void ToolStripMenuItemTeachPanelBa ...

  3. Python中参数多个值的表示法

    今天在写Python脚本时,调用了数据管理-制图综合-融合工具,在ArcGIS里操作的参数设置如下: 如果融合字段只有一个那好办,如果融合字段有多个我该怎么表达,查看帮助文档中的示例代码明白了: 所以 ...

  4. Vim使用进阶

    作为一个使用vim挺长时间的人,现在来写这篇东西确实是尴尬的,就像很多大神们说的,vim是世界上最好用的编辑器,没有之一.然后前两天又重新看了看vim的那些功能和使用方法,更觉得这么长时间使用vim却 ...

  5. 转:VB 6 在IE7以上版本机器上出现ieframe.dll 文件找不到问题

        用VB打开已存工程时弹出一个对话框:file not found c:\windows\system32\IEFRAME.dll\1 continue loading project 看到这个 ...

  6. yml 配置文件注入

    配置文件 JavaBean 自动提示 测试

  7. 解决this web application instance has been stopped already

    重启tomcat的时候出错 Illegal access: this web application instance has been stopped already.  Could not loa ...

  8. Eclipse的tomcat插件

    下载Tomcat Eclipse插件 http://www.eclipsetotale.com/tomcatPlugin.html 或者我的网盘 将tomcatPluginV321.zip内容解压到 ...

  9. eclipse查看源码失败总结

    之前看的网上查看源码的方法,查看了JDK,只是知其然不知所以然. 后来发现要是查看其他源码,总是查看失败. 最开始每次点击Attach  Source包到所要查看源码的jar包,但是还是这样. 但是依 ...

  10. 在启动vsftpd,有时会报错

    在启动vsftpd,有时会报错:C:>ftp 192.168.0.101Connected to 192.168.0.101.220 (vsFTPd 2.0.5)User (192.168.0. ...