九江市米尔网络科技有限公司
首页 | 联系方式 | 加入收藏 | 设为首页 | 手机站

产品目录

联系方式

联系人:业务部
电话: 00171-378561
邮箱:service@njrjy.com

当前位置:首页 >> 产品展示 >> 默认分类 >> 正文

SpringMVC第一个入门例子

详细信息:

这是我第一次使用SpringMVC框架的第一个例子

项目结构:


导入springmvc包

配置web.xml文件

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>SpringMVC01</display-name>
 
 <!-- 处理中文乱码 -->
 <filter>
 <filter-name>encodingFilter</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>encodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- SpringMVC控制器 -->
 <servlet>
 <servlet-name>dispatcherServlet</servlet-name>
 <!-- 主要就是DispatcherServlet这个servlet起到分发的作用,对请求进行控制分发 -->
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
 <!-- 每个springmvc项目都要一个springmvc项目配置位置,下面配置springmvc配置文件的路径 -->
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/mvc-config.xml</param-value>
 </init-param>
 <!-- 当容器启动时立即启动 -->
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>dispatcherServlet</servlet-name>
 <!-- 下面配置springmvc的过滤分发请求类型,可以是/ 或者*.action等 -->
 <url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app>


配置springmvc配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/aop
http://www.springframework.org/schema/aop/spring-aop-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 视图解析器,就是要给用户返回什么类型,视图都放在哪里?嗯,在这里 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="/hello.do" class="com.controller.HelloController">
</bean>

</beans>

编写控制类HelloController.java

package com.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 HelloController implements Controller{
 public ModelAndView handleRequest(HttpServletRequest request,
 HttpServletResponse response) throws Exception {
 // TODO Auto-generated method stub
 System.out.println("-------hello world--------");//会在控制台打印hello world
 return new ModelAndView("welcome");//并跳转到welcome这个jsp页面
 }
}

在WEB-INF下建一个jsp文件夹,在jsp文件夹下建welcome.jsp页面


<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 hello...
</body>
</html>


现在就可以测试了