1.7
在Java Resources,src/main/java中新建com.springdemo.controller.DemoController.java
代码如下:
package com.springdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/demo")
public class DemoController {
@RequestMapping("/index")
public String index(){
System.out.println(“nihao”);
return “demo”;
}
}
在src/main/java/webapp/WEB-INF下新建web.xml配置如下:
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”> spring org.springframework.web.servlet.DispatcherServlet spring / 以上配置,默认它会找serlet-name-servlet.xml,即我们配置的spring-servlet.xml 所以我们在WEB-INF下新建spring-servlet.xml,这就是springmvc的配置文件,当然我们也可以修改web.xml中的配置来把它改为通用的spring-mvc.xml 这里我们不改,就用spring-servlet.xml,代码如下 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:mvc=“http://www.springframework.org/schema/mvc” xsi:schemaLocat ion="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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> class=“org.springframework.web.servlet.view.InternalResourceViewResolver”> 视图解析器中配置了,所以我们在WEB-INF中新建view文件夹,再建demo.jsp <%@ page language=“java” contentType=“text/html; charset=UTF-8” pageEncoding=“UTF-8”%>【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取



