Spring MVC 的初步应用

1.首先导入所需的 jar包

2.在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>springMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
   <!-- 配置前端控制器 -->
  <servlet>
     <servlet-name>springmvc2</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <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>springmvc2</servlet-name>
     <url-pattern>*.action</url-pattern>    <!--配置的访问路径,一定是按照这种格式写  -->
  </servlet-mapping>
</web-app>

3.配置SpringMVC 

 3.1 方法一  手动配置

springmvc.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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
      
   <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
   </bean>
   <bean id="/hello.action" class="com.smvc.action.HController"> </bean>
                 <pre name="code" class="html">   <!-- <span style="font-family: Arial, Helvetica, sans-serif;"> 配置处理映射器 </span><span style="font-family: Arial, Helvetica, sans-serif;"> --></span>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 手动添加action的bean -->
<bean class="com.smvc.action.HController"></bean> <!-- 引入解析jstl的类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean></beans>

 

HController.java

package com.smvc.action;

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 HController implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest requset,
			HttpServletResponse response) throws Exception {
		ModelAndView m=new ModelAndView();   //向页面传数据..
		m.addObject("hello","haha");
		m.setViewName("/index.jsp");
		return m;
	}

}


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
    ${hello}<br>
  </body>
</html>

3.2 方法二  用扫描和注释

web.xml 不变

springmvc.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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
   
    <context:annotation-config>
           <mvc:annotation-driven>
           </mvc:annotation-driven>
    </context:annotation-config>
   
    <!-- 用扫描的方式把包加入到bean 中 -->
    <context:component-scan base-package="com.smvc.action"></context:component-scan>
    <!-- 引入解析jstl的类 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>

testAction.java

package com.smvc.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller

public class testAction {
	/*
	 * 注释方法
	 */
	@RequestMapping(value="/test.action")  //地址映射的注释
	public ModelAndView gettest(){
		ModelAndView m=new ModelAndView();   //向页面传值用。。。
		m.addObject("hello", "leaf");
		m.setViewName("/index.jsp");
		return m;
	}
	
}

jsp页面同上


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在现如今这个互联网以及信息技术飞速发展的时代,信息技术被广泛地运用于人们的日常生活与生产中,并有效地提高了办事、办公效率。教育信息化的高速发展,使得学校在线考试系统,作为一种新的考试管理工具,在各大院校及相关教育机构的运用日趋普遍。绝大多数的考试机构需要不断地推进自身信息化的建设,而在考务管理方面,以往手工记账、登记信息的方式,已经逐渐不能满足现如今大数据量、高效率完成的要求,使用学校在线考试系统,对学校及相关教育机构的考试事宜管理已成当下教育信息化发展的必要手段。现代化的考试机构都应该进行学校在线考试系统的替换工作,学校在线考试系统一经推出,便在考试机构范围内引起了极大地反响。 本次学校在线考试系统的实现过程,它的开发使用B/S结构即浏览器和服务器结构框架,采用SSM框架技术,数据库使用了mysql数据库,页面设计采用了MVC框架,后端采用了SSM框架技术scrip等其他一些脚本语言,使用到在大学里面学的软件工程导论课程、mysql数据库、数据库原理、SSM框架技术高级程序设计等方面的知识完成本系统。本文结合全流程网站系统开发过程,详细介绍了此次学校在线考试系统的设计理念、模型结构,将所学知识融入到本网站的开发实践中,简略介绍了研究的背景和系统存在的现实意义,开发使用的技术背景,并对系统概要设计、系统实现与系统测试等进行了详细的介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值