SpringMVC基本的配置
2014-04-01 09:55
274 查看
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring MVC配置 -->
<!-- ====================================== -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 将 springMVC-servlet.xml 放到src/config 目录下;默认路径在Web-Root目录下 -->
<param-value>classpath*:config/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>
</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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean name="/test/helloworld" class="com.cc.controller.HelloWorldController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property> <!-- 前缀,也可以理解为在什么目录下 -->
<property name="suffix" value=".jsp"></property> <!-- 后缀 -->
</bean>
</beans>
新建一个HelloWorldController继承Controller几接口,实现其方法
package com.cc.controller;
import java.util.HashMap;
import java.util.Map;
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 HelloWorldController implements Controller{
/*返回值 Model:指哪些数据 View:指返回哪给页面*/
public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {
System.out.println("hello world");
return new ModelAndView("/welcome");
}
}
新建一个JSP页面叫 welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>
This is my JSP page. <br>
<hr/>
</body>
</html>
输入网址 http://localhost:8080/SpringMVC1/test/helloworld ,显示页面,后台打印 hello world
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring MVC配置 -->
<!-- ====================================== -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 将 springMVC-servlet.xml 放到src/config 目录下;默认路径在Web-Root目录下 -->
<param-value>classpath*:config/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>
</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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean name="/test/helloworld" class="com.cc.controller.HelloWorldController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property> <!-- 前缀,也可以理解为在什么目录下 -->
<property name="suffix" value=".jsp"></property> <!-- 后缀 -->
</bean>
</beans>
新建一个HelloWorldController继承Controller几接口,实现其方法
package com.cc.controller;
import java.util.HashMap;
import java.util.Map;
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 HelloWorldController implements Controller{
/*返回值 Model:指哪些数据 View:指返回哪给页面*/
public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {
System.out.println("hello world");
return new ModelAndView("/welcome");
}
}
新建一个JSP页面叫 welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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>
This is my JSP page. <br>
<hr/>
</body>
</html>
输入网址 http://localhost:8080/SpringMVC1/test/helloworld ,显示页面,后台打印 hello world
相关文章推荐
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(2)
- spring mvc 第一天【注解实现springmvc的基本配置】
- SpringMVC系列之基本配置使用(一)
- springMVC基本配置
- springmvc笔记(基本配置,核心文件,路径,参数,文件上传,json整合)
- springMVC基本配置
- springmvc学习整理(一) springmvc的基本配置
- SpringMVC开发环境基本配置
- 基于maven的springMVC web项目的一些基本配置:
- SpringMVC基本配置
- springMVC_基本开发及配置
- springMVC入门总结(一)概念及基本配置
- springmvc入门与基本配置
- springMVC基础版--01基本配置
- 一、springMVC的基本配置
- SpringMVC基本配置
- SpringMVC基本配置
- SpringMVC基本配置
- maven与springMVC之基本配置
- SpringMVC基本配置