SpringMVC入门运行成功的实例(一)

环境:Spring 4.1.0 + JDK 1.8 + tomcat9 +Myeclipse 2016 CISpringMVC入门运行成功的实例(一)

一、配置web.xml,以启动Spring的核心Servlet

<?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" version="3.0">
  <display-name>springmvctest</display-name>
<!-- 第一种方式  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param> -->
  <!-- 第二种方式-->
    <servlet>
        <servlet-name>
spring</servlet-name>
        <servlet-class>
org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>
contextConfigLocation</param-name>
            <param-value>
/WEB-INF/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>
1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>
spring</servlet-name>
        <url-pattern>
*.do</url-pattern>
    </servlet-mapping>
</web-app>

二、配置Spring的xml文件,spring-mvc.xml,注意位置与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:tx="http://www.springframework.org/schema/tx"  
    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/tx   
    http://www.springframework.org/schema/tx/spring-tx-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"> 
    <!-- 自动扫描的包名 -->
    <context:component-scan base-package="com.model"></context:component-scan>
    
    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />
    
    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven/>
    
    <!-- 视图解释类 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean> 

</beans>

三、建立jsp文件

与spring-mvc.xml中的配置要对应,在webroot目录下建立jsp目录,然后在jsp目录下再建立hello.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    hello 
    <%System.out.println("lllllllllllllllllll............"); %>
    <br>
  </body>
</html>

四、写控制器java文件,mvcController.java

因为采用的注解方式,所以控制器的名字应该可以随便改。

但是在类上面应该加@Controller,mvc表示访问路径,hello-1表示url中要以hello-1.do访问,dohello方法可以随便命名,注解后,控制器会自动根据hello-1找到对应的该函数,return hello表示要返回hello.jsp文件。

package com.model;

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

@Controller
@RequestMapping("/mvc")

public class mvcController {
    @RequestMapping("/hello-1")
    public String dohello()
    {
        System.out.println("2333333322");
        return "hello";
    }
}
五、访问测试

浏览器使用http://localhost:8080/springmvctest/mvc/hello-1.do访问进行测试,必须带.do,要不然访问没有反应。

 

备注:

1.复制spring的xml文件,里面有非法字符,启动tomcat都会报错。需要重新整理空字符串。

2.出现了访问servlet,控制台有输出,但是页面没出来的问题。  检查jsp路径也是在jsp目录下,xml配置也没有问题。

使用@RestController注解(相当于@controller和@responsebody),得到返回页面显示hello字符串。没有问题。

从url直接访问http://localhost:8080/baoxiao/jsp/hello.jsp,没有问题。

但是http://localhost:8080/baoxiao/mvc/hello.do,得到的返回路径确实下面的,究竟是咋回事呢?目前还没有解决。。。

SpringMVC入门运行成功的实例(一)

重新配置springmvc.xml,不再设置到web-inf/jsp目录下,而是/jsp/

SpringMVC入门运行成功的实例(一)

@Controller
@RequestMapping("/mvc")
public class Dao_Bxd { 
    @RequestMapping("/hello")
    public ModelAndView dohello()
    {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate tmp=(JdbcTemplate)ctx.getBean("jdbcTemplate");
        BeanPropertyRowMapper<Bxd> rowmap=new BeanPropertyRowMapper<Bxd>(Bxd.class);
        String sql="select id,bx_id as bx_code from cw_bxd limit 3 ";
        List<Bxd> bxds=tmp.query(sql, rowmap);
        for(Bxd bxd:bxds)
        {
            System.out.println(bxd.getBx_code());
        }
        return new ModelAndView("Hello");
    }
    
    @RequestMapping("/hello1")
    public String dohello1()
    {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate tmp=(JdbcTemplate)ctx.getBean("jdbcTemplate");
        BeanPropertyRowMapper<Bxd> rowmap=new BeanPropertyRowMapper<Bxd>(Bxd.class);
        String sql="select id,bx_id as bx_code from cw_bxd limit 3 ";
        List<Bxd> bxds=tmp.query(sql, rowmap);
        for(Bxd bxd:bxds)
        {
            System.out.println(bxd.getBx_code());
        }
        return "Hello1";
    }    

url访问路径:http://localhost:8080/baoxiao/mvc/hello.do