SpringMVC-1 第一个SpringMVC程序

1.需求:

  • 用户提交一个请求,服务端处理器接收到请求后,给出一条信息, 在相应页面中显示该条信息

2.搭建环境

  • 2.1导入jar包
  • 2.2配置web.xml,注册SpringMVC前端控制器(中央调度器)
  • 2.3编写SpringMVC后端控制器
  • 2.4编写springmvc配置文件(注意文件名称格式),注册后端控制器 (注意id写法格式)
  • 2.5编写跳转资源页面

1.导入jar包

需要的JAR包

  1. com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
  2. com.springsource.org.aopalliance-1.0.0.jar
  3. com.springsource.org.apache.commons.logging-1.1.1.jar
  4. com.springsource.org.apache.log4j-1.2.15.jar
  5. com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  6. junit-4.9.jar
  7. mysql-connector-java-5.1.30.jar
  8. spring-aop-4.1.6.RELEASE.jar
  9. spring-aspects-4.1.6.RELEASE.jar
  10. spring-beans-4.1.6.RELEASE.jar
  11. spring-context-4.1.6.RELEASE.jar
  12. spring-context-support-4.1.6.RELEASE.jar
  13. spring-core-4.1.6.RELEASE.jar
  14. spring-expression-4.1.6.RELEASE.jar
  15. spring-jdbc-4.1.6.RELEASE.jar
  16. spring-orm-4.1.6.RELEASE.jar
  17. spring-tx-4.1.6.RELEASE.jar
  18. spring-web-4.1.6.RELEASE.jar
  19. spring-webmvc-4.1.6.RELEASE.jar

 2.配置web.xml,注册SpringMVC前端控制器(中央调度器)

<?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>01-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>
  <!-- 配置springMVC中央调度器 -->
  <servlet>
  	<servlet-name>victor</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定springmvc配置文件的路径及名称 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>victor</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

3.编写SpringMVC后端控制器

public class MyController implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.addObject("message","Hello SpringMVC World!");
		mv.setViewName("/WEB-INF/jsp/welcome.jsp");
		return mv;
	}

}

4.编写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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
     <!-- 注册后端控制器 -->   
     <bean id="/my.do" class="com.bjsxt.handlers.MyController"></bean>
</beans>

5.编写跳转资源页面

<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
	${message}
</body>
</html>

 

6.目录结构

163023_3Cd2_3519338.png

 

3.web.xml中urlpattern配置问题

 

3.1配置/和配置/*的区别 162450_BPqC_3519338.png

3.2静态资源无法访问解决方案(三种)

此方法针对于/*

1. 在web.xml中增加如下配置


  <servlet-mapping>
      <servlet-name>default</servlet-name>
      <url-pattern>*.png</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
      <servlet-name>default</servlet-name>
      <url-pattern>*.css</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
      <servlet-name>default</servlet-name>
      <url-pattern>*.js</url-pattern>
  </servlet-mapping>

2.第二种方案

第一步 增加头信息

        xmlns:mvc="http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd

第二步 配置文件中增加<mvc:default-servlet-handler/>

例:

<?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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <mvc:default-servlet-handler/>
     <!-- 注册后端控制器 -->   
     <bean id="/my.do" class="com.bjsxt.handlers.MyController"></bean>
</beans>

3.第三种方案

第一步  也是增加头信息

第二步 使用<mvc:resources location="" mapping=""/>

如<mvc:resources location="/images/" mapping="/images/**"/>

其中/images/**后面的**表示任意文件

4.视图解析器

<!-- 注册视图解析器 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/WEB-INF/jsp/"/>
         <property name="suffix" value=".jsp"/>
     </bean>

转载于:https://my.oschina.net/nan99/blog/1518142

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值