admin管理员组文章数量:1794759
Spring MVC 配置XML+注解
下面我将阐述两种方法 配置XML和注解 的方法 来使用SpringMVC
1.导入jar包
2.配置web.xml文件 3.书写配置文件(配置xml方式)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xmlns:aop="www.springframework/schema/aop" xmlns:tx="www.springframework/schema/tx" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-4.0.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-4.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-4.0.xsd www.springframework/schema/aop www.springframework/schema/aop/spring-aop-4.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx-4.0.xsd "> <!-- spring MVC 核心配置文件 1.处理器映射器 2.处理器配置器 3.视图解析器 --> <!-- 处理映射器 --> <!-- 1.根据bean的name进行查找Handler 将action的url配置在bean的name中 --> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> --> <!-- 2.简单url映射 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <!-- /demo1.action /demo2.action 都可以访问到指定的hanler1 --> <prop key="/demo1.action">userhandler1</prop> <prop key="/demo2.action">userhandler1</prop> <prop key="/demo3.action">userhandler2</prop> <prop key="/demo4.action">userhandler2</prop> </props> </property> </bean> <!-- 处理适配器 --> <!-- 1.处理器适配器 配置SimpleControllerHandlerAdapter ,要求handler必须实现Controller接口 --> <!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> --> <!-- 2.处理器适配器 配置HttpRequestHandlerAdapter ,要求handler必须实现HttpRequestHandler接口 --> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/> <!-- 视图解析器 --> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 前缀:/WEB-INF/jsp/ 后缀:.jsp /WEB-INF/jsp/list.jsp --> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置bean --> <bean name="/demo1.action" id="userhandler1" class="com.baidu.handler.UserHandler1"></bean> <bean id="userhandler2" class="com.baidu.handler.UserHandler2"></bean> </beans>测试的handler类1:
package com.baidu.handler; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class UserHandler1 implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // ModelAndView modelAndView = new ModelAndView(); //传值 modelAndView.addObject("name", "肥龙"); //跳转页面 modelAndView.setViewName("list"); System.out.println("测试请求1.。。。。。。。。。。"); return modelAndView; } }测试的handler类2:
package com.baidu.handler; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.HttpRequestHandler; import org.springframework.web.servlet.ModelAndView; public class UserHandler2 implements HttpRequestHandler { public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ModelAndView modelAndView = new ModelAndView(); //response.setCharacterEncoding("utf-8"); //response.setContentType("application/json;charset=utf-8"); //response.getWriter().write("json串"); System.out.println("请求来了 哈哈哈 这是第二个 "); } }3.书写配置文件(配置注解方式一)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xmlns:aop="www.springframework/schema/aop" xmlns:tx="www.springframework/schema/tx" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-4.0.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-4.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-4.0.xsd www.springframework/schema/aop www.springframework/schema/aop/spring-aop-4.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx-4.0.xsd "> <!-- 扫描包 --> <context:component-scan base-package="com.baidu"></context:component-scan> <!--注解映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!--注解适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!-- 视图解析器 --> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 前缀:/WEB-INF/jsp/ 后缀:.jsp /WEB-INF/jsp/list.jsp --> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>测试handler:
package com.baidu.handler; import java.io.IOException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping(value="/user3") public class UserHandler3{ @RequestMapping(value= {"/list.action","/list2.action"}) public String list() { System.out.println("请求来了 这是333 使用注解"); return "list"; } }3.书写配置文件(配置注解方式二)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:mvc="www.springframework/schema/mvc" xmlns:context="www.springframework/schema/context" xmlns:aop="www.springframework/schema/aop" xmlns:tx="www.springframework/schema/tx" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-4.0.xsd www.springframework/schema/mvc www.springframework/schema/mvc/spring-mvc-4.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-4.0.xsd www.springframework/schema/aop www.springframework/schema/aop/spring-aop-4.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx-4.0.xsd "> <!-- 扫描包 --> <context:component-scan base-package="com.baidu"></context:component-scan> <!-- 牛逼配置 --> <mvc:annotation-driven/> <!-- 视图解析器 --> <!-- ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- 前缀:/WEB-INF/jsp/ 后缀:.jsp /WEB-INF/jsp/list.jsp --> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>测试handler:
package com.baidu.handler; import java.io.IOException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping(value="/user3") public class UserHandler3{ @RequestMapping(value= {"/list.action","/list2.action"}) public String list() { System.out.println("请求来了 这是333 使用注解"); return "list"; } }总结: xml配置 可以修改处理映射器和处理适配器来使用 但是 视图解析器不会变
注解 可以使用有两种模式 但是 视图解析器也不会变
版权声明:本文标题:Spring MVC 配置XML+注解 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686618735a86932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论