springmvc笔记-1-搭建
来源:互联网 发布:java 合并多个excel 编辑:程序博客网 时间:2024/06/07 14:39
springmvc搭建:
spring的jar下载
common-logging的jar下载
在mvc框架中提供了一个servlet:org.springframework.web.servlet.DispatcherServlet,所有的请求,都是通过它来进行请求分派
在web.xml中配置如下:
<servlet> <!-- classpath:WEB-INF/springmvcNote1-servlet.xml --> <!-- servlet的名称 --> <servlet-name>springmvcNote1</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 下面配置类servlet的参数信息 在这里配置了xml 那就必须加上springmvcNote1-servlet.xml 应该是覆盖了 - contextConfigLocation是参数名称 该参数值param-value包含了springmvc配置文件的路径 -> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml,/WEB-INF/springmvcNote1-servlet.xml</param-value> </init-param> <!-- web应用启动时立即加载servlet --> <load-on-startup>1</load-on-startup> </servlet> <!-- servlet的映射申明 --> <servlet-mapping> <!-- 对应的servlet名称 --> <servlet-name>springmvcNote1</servlet-name> <!-- 监听当前域的所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping>
由于上面配置的servlet名称是springmvcNote1
故会在WEB-INF下面找springmvcNote1-servlet.xml文件作为springmvc的配置文件(该文件默认是放在WEB-INF目录下的),解析该文件创建一个WebApplicationContext容器对象,称为“上下文环境”
springmvcNote1-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:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd" default-lazy-init="false" > <!-- 通过那么的值进行对应拦截 比喻输入http://localhost:8080/springmvcNote1/input 那么就进入 InputProductController这个类 对应的InputProductController必须实现Controller接口 注解便不用实现这个接口 --> <bean name="/input" class="com.web.InputProductController"></bean> <!-- <bean name="/save" class="com.three.SaveProductController"></bean> --> <!-- 是一个处理器适配器,所有处理器适配器都要实现HandlerAdaper接口 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>
对应的Controller实现类
package com.web;import javax.naming.ldap.Control;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;/* * 因为是xml配置的,所以必须实现Controller接口 */public class InputProductController implements Controller{ //xml配置就只能是这个方法 @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { //配置准备返回的ModeAndView对象,该对象通常包含了返回视图名,模型的名称,以及模型的对象 ModelAndView mv = new ModelAndView(); //添加模型数据,可以是任意类型的对象 mv.addObject("message","Hello World!"); //设置视图名,配合springmvcNote1-servlet.xml中的视图解析器,合成指向/WEB-INF/jsp/test.jsp页面 mv.setViewName("test"); return mv; }}
WEB-INF/jsp目录下的test.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>${requestScope.message }</body></html>
访问测试的url:http://localhost:8080/springmvcNote1/input
对应的注解方式
在springmvcNote1-servlet.xml中添加配置:
<!-- 扫包 ,将所有spring的注解注册成bean--> <context:component-scan base-package="com.**"></context:component-scan> <!-- 配置annotation类型的处理映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <!-- 配置annotation类型处理器适配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
对应的Controller类
package com.web;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class HelloController { @RequestMapping(value="/hello") public ModelAndView hello() { // 配置准备返回的ModeAndView对象,该对象通常包含了返回视图名,模型的名称,以及模型的对象 ModelAndView mv = new ModelAndView(); // 添加模型数据,可以是任意类型的对象 mv.addObject("message", "Hello World!"); // 设置视图名,配合springmvcNote1-servlet.xml中的视图解析器,合成指向/WEB-INF/jsp/test.jsp页面 mv.setViewName("test"); return mv; }}
访问测试的url:http://localhost:8080/springmvcNote1/hello
阅读全文
0 0
- springmvc笔记-1-搭建
- 搭建springmvc笔记
- 【笔记】SpringMVC---SpringMVC和简单环境搭建
- Spring学习笔记1:SpringMVC环境的搭建
- springmvc学习笔记(1)——搭建springmvc项目之HelloWorld
- springMVC环境搭建-1
- 【JAVAWEB学习笔记】springmvc环境搭建
- 搭建SpringMVC学习笔记(一)
- springMVC学习笔记(一) ---- 搭建框架
- SpringMVC学习笔记(二)——SpringMVC开发环境搭建
- SpringMVC个人笔记1
- SpringMvc笔记1
- springmvc笔记(1)
- SpringMVC自学笔记1
- springmvc学习笔记《1》
- springMVC 问题笔记1
- SpringMVC学习笔记1
- SpringMVC学习笔记1
- [WUA APIs]InstallationResult
- Linux tail命令 监控日志的神器
- [WUA APIs]SearchResult
- Windows Server 2008使用无线网卡上网
- RN开工
- springmvc笔记-1-搭建
- 231. Power of Two
- POJ
- HDU-1398-Square Coins(完全背包)
- Oulipo 、Power Strings (KMP入门)
- 几行代码搞定Android底部导航栏
- 《机器学习》阅读心得——六、支持向量机
- php语言实现tcpip协议
- IaaS、Paas、Saas