springmvc入门

springMVC与Tomcat建立联系:

 

eclipse是2020.12版;Tomcat是8.5;

未使用maven;

总目录:

首先,建立web工程:

 1.修改生成的web.xml文件:标签<param-value>classpath:spring-mvc.xml</param-value>“spring-mvc”和自己的文件名要对应;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  
<display-name>springMVC01</display-name>
  <welcome-file-list>
<welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
</init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

 2.建立spring-mvc.xml文件:    <context:component-scan base-package="com.css"/>,需要和自己的包名对应; 

<mvc:annotation-driven />
<mvc:default-servlet-handler />

这两个标签我做时是有顺序的,不然会有:no mapping for GET+文件名;

<?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"
    
    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-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        ">                    
    
    <context:component-scan base-package="com.css"/>
    
    <mvc:annotation-driven /> 
    <mvc:default-servlet-handler />
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
    
</beans>

3.在src下建立测试类:输出语句是为了测试是否连接了Tomcat;

package com.css;

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

@Controller
public class test {
	@RequestMapping("/xixi")
	public String z() {
		
		System.out.println("1111");
		
		return "bbbb";
	}
}

4.两个前端页面,请求和跳转;

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>如果提醒,可能是没有导入“servlet-api.jar”包;

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="xixi">gogogo</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

测试:输入路径:http://127.0.0.1:8080/springMVC01/index.html

连接跳转

同时控制台输出信息:控制台输出,证明经过Tmocat;

 至此是建立完成。

提示:jar包需要放在lib目录下,我是复制到lib文件夹下的,因为通过Build Path,加载不到lib目录下,会有异常;除此之外,任何一点问题都有可能异常,这个案例如果不能正常执行,可能是我复制粘贴有问题,建议多看几份,如果搞懂代码就可以任意变换了;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值