Spring mvc 入门demo
来源:互联网 发布:黑龙江优化发展环境 编辑:程序博客网 时间:2024/05/13 16:48
环境 Eclipse Jee Neon。
需要Spring MVC 的Jar包
Demo-01 :
工程结构:
web.xml 文件内容:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.5"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_2_5.xsd"> <display-name>springmvc_demo_02</display-name> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如SpringMVC-servlet.xml <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/SpringMVC-servlet.xml</param-value></init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
SpringMVC-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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><context:component-scan base-package="org.springframework.samples.petclinic.web" /><bean name="/hello" class="com.feizi.controller.HelloController"></bean><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean></beans>
index.jsp 文件内容:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> This is my JSP page. <br> </body></html>
hello.jsp 文件内容:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'hello.jsp' starting page</title> </head> <body> <h3>shit , 这是hello.jsp</h3> </body></html>
package com.feizi.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;@org.springframework.stereotype.Controllerpublic class HelloController {@RequestMapping("hellonn")public ModelAndView handleRequest() throws Exception {System.out.println("wocao , 进入RequestMapping(\"hello\")...");System.out.println("shit");//实际上就是跳转到hello.jsp页面return new ModelAndView("/index");}}
demo-02 、 demo-03 与 demo-01 大同小异,关键不同部分;
@org.springframework.stereotype.Controllerpublic class HelloController {@RequestMapping("index")public ModelAndView handleRequest() throws Exception {System.out.println("======================== IN handlerRequest()...==============================");/*String firstParam = "第一个字符串参数";return new ModelAndView("/hello", "prm", firstParam);//字符串用*/ Map<String, Object> map = new HashMap<String, Object>(); map.put("prm1", "map中的第一个"); map.put("prm2", "map中的第二个"); map.put("prm3", "map中的第三个"); return new ModelAndView("/hello", "prm", map);}}和
@Controllerpublic class HelloController {@RequestMapping(value="/hello",method=RequestMethod.GET)public String sayHello(){System.out.println("======================Hello World!!!================");//直接返回字符串,也是通过解析,映射到相应的hello.jsp。return "hello";}}还有xxx-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置注解的支持 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 启用自动扫描 --> <context:component-scan base-package="com.feizi.controller"></context:component-scan> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--此处的前缀不再是/, 而是value="/WEB-INF/jsp/". <property name="prefix" value="/"></property> --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- 配置视图类型为jsp类型 --> <property name="suffix" value=".jsp"></property> </bean></beans>
0 0
- Spring MVC入门Demo
- Spring mvc 入门demo
- spring MVC入门示例(hello world demo)
- spring、spring mvc 3.2 最全入门进阶Demo
- spring、spring mvc 3.2 最全入门进阶Demo
- spring mvc demo
- spring mvc demo
- spring mvc简单demo
- Spring mvc 单元测试Demo
- spring mvc demo
- spring mvc demo案例
- 【spring】spring MVC入门
- 【Spring】Spring MVC入门
- Spring入门Demo
- spring security3 demo入门
- spring + Quartz 入门demo
- Spring AOP入门Demo
- Spring Security入门Demo
- S3C2410 Linux kernel 移植记录
- sicily Sorting Algorithm
- 操作系统算法 之 SCAN磁盘调度算法
- 并发控制
- 路虽远,行则将至——12月份英语总结
- Spring mvc 入门demo
- 大二上学期实训 图书管理系统
- iOS无法使用自定义密码输入控件(自带键盘)
- 设计心理学之色彩心理学和马斯洛需求层次理论
- HTML+CSS实现圆圈里面有个数字
- Ubuntu apt-get 国内源
- ZCMU-1162-松哥的素数
- SpringBoot使用笔记
- selenium+python京东自动登录及秒杀