一、springMVC的基本配置
2014-01-28 16:41
369 查看
1.web.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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">
<!-- 处理器配置的方式实现 ,之后用注解的方式代替 -->
<bean name="/welcome" class="com.web.controller.HelloWorldController"></bean>
<!-- ViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.建个jsp文件 welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
<c:forEach items="${map}" var="m">
${m.key} ---> ${m.value}
</c:forEach>
</body>
</html>
4.引入jar包
其中commons-logging-1.1.1.jar是spring日志依赖jar包,jstl-api-1.2.jar与jstl-impl-1.2.jar是oracle官方的jstl接口与实现
5.后台实现
public class HelloWorldController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
Map<String , Object> map = new HashMap<String, Object>();
map.put("1", "one");
map.put("2", "bb");
map.put("3", "cc");
return new ModelAndView("/welcome","map",map);
}
}
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- POST中文乱码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 自定义路径 --> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>2.在config包下配置spring-servlet.xml,文件名要与<servlet-name></spring-name>的前缀*一样
<?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="/welcome" class="com.web.controller.HelloWorldController"></bean>
<!-- ViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
3.建个jsp文件 welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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>
<c:forEach items="${map}" var="m">
${m.key} ---> ${m.value}
</c:forEach>
</body>
</html>
4.引入jar包
其中commons-logging-1.1.1.jar是spring日志依赖jar包,jstl-api-1.2.jar与jstl-impl-1.2.jar是oracle官方的jstl接口与实现
5.后台实现
public class HelloWorldController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
Map<String , Object> map = new HashMap<String, Object>();
map.put("1", "one");
map.put("2", "bb");
map.put("3", "cc");
return new ModelAndView("/welcome","map",map);
}
}
相关文章推荐
- SpringMVC 学习- Spring MVC 框架及基本配置
- SpringMVC,Struts2,Hibernate,Mybatis基本配置以及他们的核心架包pom配置
- (不推荐使用)springMVC基本配置+继承MultiActionController来实现根据参数名指定要请求的方法
- 从零开始···springMVC基本配置(很糙)
- SpringMVC 基本配置文件
- SpringMVC基本的配置
- springmvc 项目:web.xml基本配置
- SpringMVC系列之基本配置
- 最基本的springMVC配置文件详解
- springmvc基本配置
- SpringMVC 入门基础和基本配置
- springmvc 项目:web.xml基本配置
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
- springMVC学习笔记---注解启用基本配置
- SpringMVC-基本配置
- 【SpringMVC】SpringMVC基础-静态资源映射、拦截器配置、@ControllerAdvice与其他基本配置
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
- springMVC-servlet.xml配置文件的基本配置
- springmvc的基本配置