Spring MVC 的初步应用
来源:互联网 发布:淘宝客服的上班时间 编辑:程序博客网 时间:2024/05/21 11:05
1.首先导入所需的 jar包
2.在web.xml里配置前端控制器和拦截器
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="
http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springMVC</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> <!-- 配置前端控制器 --> <servlet> <servlet-name>springmvc2</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc2</servlet-name> <url-pattern>*.action</url-pattern> <!--配置的访问路径,一定是按照这种格式写 --> </servlet-mapping></web-app>
3.配置SpringMVC
3.1 方法一 手动配置
springmvc.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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" 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.xsd"> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"> </bean> <bean id="/hello.action" class="com.smvc.action.HController"> </bean> <pre name="code" class="html"> <!-- <span style="font-family: Arial, Helvetica, sans-serif;"> 配置处理映射器 </span><span style="font-family: Arial, Helvetica, sans-serif;"> --></span><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 手动添加action的bean-->
<bean class="com.smvc.action.HController"></bean> <!-- 引入解析jstl的类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean></beans>
HController.java
package com.smvc.action;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 HController implements Controller{@Overridepublic ModelAndView handleRequest(HttpServletRequest requset,HttpServletResponse response) throws Exception {ModelAndView m=new ModelAndView(); //向页面传数据..m.addObject("hello","haha");m.setViewName("/index.jsp");return m;}}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>springMVC</title> </head> <body> ${hello}<br> </body></html>
3.2 方法二 用扫描和注释
web.xml 不变
springmvc.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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" 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.xsd"> <context:annotation-config> <mvc:annotation-driven> </mvc:annotation-driven> </context:annotation-config> <!-- 用扫描的方式把包加入到bean 中 --> <context:component-scan base-package="com.smvc.action"></context:component-scan> <!-- 引入解析jstl的类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean></beans>
testAction.java
package com.smvc.action;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class testAction {/* * 注释方法 */@RequestMapping(value="/test.action") //地址映射的注释public ModelAndView gettest(){ModelAndView m=new ModelAndView(); //向页面传值用。。。m.addObject("hello", "leaf");m.setViewName("/index.jsp");return m;}}
jsp页面同上
0 0
- Spring MVC 的初步应用
- Spring MVC的初步搭建(应用篇)
- spring MVC框架的初步搭建
- spring-mvc的初步认识
- Spring MVC上的应用
- spring的简介与初步搭建应用
- Spring MVC框架初步搭建
- Spring---Spring MVC 应用
- spring mvc的HttpSession应用实例
- spring-mvc 的 拦截器应用例子
- Springboot构建spring mvc的web应用
- Spring Web MVC简介 、基于XML配置的MVC应用 、基于注解配置的MVC应用
- spring mvc应用
- 框架应用 : Spring MVC
- Spring-MVC初步-搭建一个web程序
- 对MVC的初步理解
- 对MVC的初步理解
- Spring的初步认识
- 编程的智慧
- 设计模式六大原则(2):里氏替换原则
- 多个TextView 跑马灯效果
- POJ 3040Allowance【难,贪心】
- jqgrid基础知识了解
- Spring MVC 的初步应用
- Android Handler到底是什么?
- POJ 2528 Mayor's posters ( 线段树 : 区间修改 + 离散化 )
- Python学习(Leetcode 算法题解【21~40】)
- java中super关键字的理解(自己测试得出的结论)
- 【HDU】-2035-人见人爱A^B(快速幂)
- centos sonar Unable to start JVM: No such file or directory
- 【HDU】2824 - GCD(欧拉函数打表)
- Key Set(快速幂)