spring MVC 初探 (HelloWorld)
1、使用spring MVC 需要导入相关jar包
2、web.xml 启用spring MVC
<servlet>
<servlet-name>spring3mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定配置文件所在目录src 默认目录在/WEB-INF/ 默认文件名<servlet-name>-servlet.xml -->
<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>spring3mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3、编写Controller 使用xml配置文件方式需要继承Controller
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 HelloWorld implements Controller { @Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String value = "nihao";
//返回视图 参数1为显示页面因为后面配置文件配置了后缀所以这里指需要给出页面名称不需要后缀
// 传参使用键值对 参数会绑定到request域
return new ModelAndView("/test", "Key", value);
} }
4、显示页面test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
使用EL表达式取值
Ok!!!!!!!!!!! ${key }
</body>
5、spring MVC配置文件 单独Controlle配置
<?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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<bean name="/test" class="com.ly.Controller.HelloWorld"></bean> <!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀就是指定你的显示页面根目录 -->
<property name="prefix" value="/" />
<!-- 后缀给出显示页面后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
6、一个controller编写多个方法 需要继承 MultiActionController (同样使用xml配置文件方式)
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class multiController extends MultiActionController { public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("add");
return new ModelAndView("/test", "key", "value");
} public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("update");
return new ModelAndView("/test", "key", "value");
}
}
7、一个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: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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<bean name="/testc" class="com.ly.Controller.multiController">
<property name="methodNameResolver" ref="methodNameResolver"></property>
</bean> <!-- 一个controller 编写多个方法 解析 -->
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"></property>
</bean> <!-- 添加注解驱动 -->
<mvc:annotation-driven />
<!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀就是指定你的显示页面根目录 -->
<property name="prefix" value="/" />
<!-- 后缀给出显示页面后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
spring MVC 初探 (HelloWorld)的更多相关文章
- java之spring mvc之helloworld
这篇主要讲解springmvc的基本的使用,这里以helloworld项目为例. 目录结构: 1. 新建 web 项目 :springmvc_helloworld 2. 在 WebRoot\WEB-I ...
- 利用maven构建一个spring mvc的helloworld实例
刚开始学习maven和spring mvc,学的云里雾里的 这里提供一个hello world实例,记录自己的学习之路 首先看maven官网的介绍 Apache Maven is a software ...
- IDEA+Maven+Spring MVC HelloWorld示例
用Maven创建Web项目 选择webapp模板 创建成功后点Enable Auto-Import idea给我们创建出来的结构是这样的,这还不标准,需要自己修改. 在main文件夹下创建java文件 ...
- Spring MVC (JDK8+Tomcat8)
1 Spring MVC概述 Spring MVC是Spring为表现层提供的基于MVC设计理念的优秀的web框架,是目前最主流的MVC框架之一. Spring3.0后全面超越Struts2,成为最优 ...
- Spring mvc初学
转自:http://www.cnblogs.com/bigdataZJ/p/springmvc1.html 从今天起,准备好好审视并学习Spring mvc. 虽然从学java的第一个程序——hell ...
- Spring MVC学习纲要
感慨一下 之前用过Spring MVC, MyBatis,但是很久不用之后发现很多知识点都荒废了,毕竟工作就是重复,重复再重复.没有啥新东西.所以还是找个时间把忘了的东西捡起来.万一搞了个大bug,然 ...
- Eclipse下创建Spring MVC web程序--maven版
1. 创建一个maven工程: File->New->Other... 2. 创建完成后的结构如下: 3. 配置pom.xml文件,添加spring-webmvc依赖项 <pro ...
- spring MVC学习之二
什么是Spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 MV ...
- 【WEB】初探Spring MVC框架
Spring MVC框架算是当下比较流行的Java开源框架.但实话实说,做了几年WEB项目,完全没有SpringMVC实战经验,乃至在某些交流场合下被同行严重鄙视“奥特曼”了.“心塞”的同时,只好默默 ...
随机推荐
- kill -0
http://unix.stackexchange.com/questions/169898/what-does-kill-0-do 检查有没有权限杀他
- Echarts自适应浏览器大小
var myChart = echarts.init(document.getElementById('sitesChar')); var option = { title : { text: 'No ...
- php moungoDB
moungoDB 语法 SQL查询语句 Mongo查询语句 CREATE TABLE USERS (a Number, b Number) 隐式的创建,或 MongoDB::createCollect ...
- [转]new一个Object对象占用多少内存?
我们分解下ArrayList arr = new ArrayList();等同于ArrayList arr = null;//初始化arr = new ArrayList();//实例化这两个过程.初 ...
- linux shell 远程执行命令
经常要部署多台服务器上面的应用,如果一个个机器的登录太麻烦. 所有就想到编写一个脚本来部署不同的服务器 前提条件: 配置ssh免登陆 如果不会的请参加我的另外一篇文章 http://blog.csdn ...
- Air打包exe
1.用flash创建一个airtest.fla,发布目标选择为AIR.ctrl+enter会得到如下文件: 2.把flex sdk的bin中找到adl.exe,复制过来,放置到:项目目录\bin\ad ...
- NYOJ 925 国王的烦恼
从最后一天开始往前加边. 同一天的边同时加到图上,加完之后检查集合数量是否和没加之前有变化. 有变化的话,答案就+1. #include<cstdio> #include <iost ...
- CodeForces 190D Non-Secret Cypher
双指针. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- 在Visual C++中的用ADO进行数据库编程
1. 生成应用程序框架并初始化OLE/COM库环境 创建一个标准的MFC AppWizard(exe)应用程序,然后在使用ADO数据库的InitInstance函数中初始化OLE/COM库(因为ADO ...
- 优化之sitemap+RSS
RSS也叫聚合, RSS是在线共享内容的一种简易方式,也叫聚合内容,Really Simple Syndication. 通常在时效性比较强的网站或网络平台上应用RSS订阅功能可以更快速获取信息,网站 ...