狂神的servlet回顧就不在這里寫(xiě)了。可以翻之前的筆記。

創(chuàng)建webapp的maven項(xiàng)目。

也就是四個(gè)文件 (不用思考里面的代碼,直接CV先走一遍流程)
HelloController:
package com.Li.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//注意:這里我們先導(dǎo)入Controller接口
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//ModelAndView 模型和視圖
ModelAndView mv = new ModelAndView();
//封裝對(duì)象,放在ModelAndView中。Model
mv.addObject("msg","HelloSpringMVC!");
//封裝要跳轉(zhuǎn)的視圖,放在ModelAndView中
mv.setViewName("hello"); //: /WEB-INF/jsp/hello.jsp
return mv;
}
}
springmvc-servlet.xml:
hello.jsp:
<%--
Created by IntelliJ IDEA.
User: Li
Date: 2022/12/1
Time: 17:23
To change this template use File | Settings | File Templates.
--%><%@ page contentType="text/html;charset=UTF-8" language="java" %>Title ${msg}web.xml:
springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml 1 springmvc / pom.xml(外面那個(gè)):
junit junit4.12 org.springframework spring-webmvc5.1.9.RELEASE javax.servlet servlet-api2.5 javax.servlet.jsp jsp-api2.2 javax.servlet jstl1.2 配置完tomcat之后直接run。之后路徑上輸入hello測(cè)試
404問(wèn)題:


如果沒(méi)有l(wèi)ib,添加一個(gè)lib文件夾。

選中l(wèi)ib點(diǎn)+號(hào),之后點(diǎn)第一個(gè)

將這些全部導(dǎo)入,apply之后OK即可。
大致分為這三部分,對(duì)照視頻理解每個(gè)文件級(jí)代碼塊的作用。


HelloController:
package com.Li.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
//@RequestMapping("/hello")看第11行的注釋,如果這行不注釋的話,就是11行的效果
public class HelloController {
//localhost:8080/hello/h1
@RequestMapping("/h1")
public String hello(Model model){
//封裝數(shù)據(jù),向模型中添加值,可以在JSP頁(yè)面取出并渲染
model.addAttribute("msg", "Hello,SpringMVCAnnotation");
return "hello";//會(huì)被視圖解析器處理
}
}springmvc-servlet.xml:
hello.jsp:
<%--
Created by IntelliJ IDEA.
User: Li
Date: 2022/12/2
Time: 22:55
To change this template use File | Settings | File Templates.
--%><%@ page contentType="text/html;charset=UTF-8" language="java" %>Title ${msg}web.xml:
SpringMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:springmvc-servlet.xml 1 SpringMVC / 測(cè)試:

重點(diǎn)在于理解,代碼都很簡(jiǎn)單。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)站標(biāo)題:初識(shí)springmvc-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)于:http://www.chinadenli.net/article30/iehpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、小程序開(kāi)發(fā)、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容