Spring MVC 的xml一些配置
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一些配置的更多相关文章
- Spring MVC 的 XML 配置方式
索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml solution/webapi/pom.xml solution/mapper/ ...
- Spring MVC Web.xml配置
Web.xml spring&spring mvc 在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个 ...
- Spring MVC 返回 xml json pdf 数据的配置方法
<!-- Spring MVC 返回 xml 数据的配置方法 --> <bean class="org.springframework.web.servlet.vi ...
- Spring MVC 使用tomcat中配置的数据源
Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources&g ...
- Spring mvc web.xml中 urlpatten的配置问题
在使用spring mvc 是我们会配置spring 的DispatcherServlet作为请求的转发器. <servlet> <servlet-name>spring< ...
- Spring配置文件beans.xml头部配置解释
Spring配置文件beans.xml头部配置解释 - EasonJim - 博客园https://www.cnblogs.com/EasonJim/p/6880329.html
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- Spring MVC 3.x 基本配置
WEB-INF/web.xml 例1 <?xml version="1.0" encoding="UTF-8"?> <web-app xmln ...
- 使用高性能xml序列化框架jibx作为spring mvc的xml view
package org.springframework.web.servlet.view.xml; import java.io.ByteArrayOutputStream; import java. ...
随机推荐
- 以上帝模式管理Windows系统
上帝模式,,即"God Mode",或称为"完全控制面板".是Windows Vista/7系统中隐藏的一个简单的文件夹窗口,但包含了几乎所有Windows系统 ...
- 关于json与protobuf的材料
1. https://solicomo.com/network-dev/protobuf-proto3-vs-proto2.html 2.
- Untracked Files Prevent Checkout move or commit them before checkout
点开View Files... 查看里面的文件名称,在项目的.idea文件夹中删掉ViewFiles显示的文件夹名称就好
- 一个简单RPC框架是怎样炼成的(IV)——实现RPC消息的编解码
之前我们制定了一个非常easy的RPC消息 的格式,可是还遗留了两个问题,上一篇解决掉了一个.还留下一个 我们并没有实现对应的encode和decode方法,没有基于能够跨设备的字符串传输,而是直接的 ...
- 使用Python脚本批量裁切栅格
对栅格的裁切,我们通常使用裁切(数据管理-栅格-栅格处理)或按掩膜提取(空间分析-提取分析)来裁切,裁切的矢量要素通常是一个要素图层或Shape文件.如果要进行批量处理,可以使用ToolBox中的批量 ...
- Asp.Net 之 <%%>相关内联代码块用法
1.<%@ ... %> 用来添加命名空间引用,如:<%@ import namespace="system.data"> 2.<% ... %> ...
- 【JavaScript】实现复选框的全选、全部不选、反选
以较为简洁的程序实现复选框的全选.全部不选.反选 操作. 并且将可变的部分设置为JS的参数,以实现代码复用. 全选和全不选 第一个参数为复选框名称,第二个参数为是全选还是全部不选. function ...
- Discuz的sc 和tc版本有什么区别
Discuz的sc 和tc版本有什么区别 简单的来说: sc 是tc 是繁体中文 简体中文Simplified Chinese 繁体中文traditional Chinese
- MySQL优化小案例:连接数
错误代码:MySQL: ERROR 1040: Too many connections 经常会遇到这个错误,要么是业务增长,正常的访问量增多,要么是自己的max_connections设置的过小了 ...
- 无障碍阅读:页面缩放兼容性处理(zoom,Firefox火狐浏览器)
1.无障碍阅读使用场景 无障碍阅读一般在政府类网站使用比较多,如: 天津海事局(http://www.tjmsa.gov.cn/),其中天津海事局的页面放大和页面缩小在firefox浏览器下存在bug ...