java web实现hellojava,Spring MVC 简单的hello world的实现

一、项目搭建

1、可以在新建项目的使用Spring MVC框架。或者创建一个简单的项目之后再用Add Framework Support来添加Spring MVC框架。

9f9ebee385f7a2da8e81053c8a601cc1.png

2、删除自动生成的lib的jar包,使用pom文件来进行管理包。目录结构如下图。

8c0ff54f7a6b900c7daf3cfbc975d7c1.png

3、pom文件。加载完成之后才能进行下一步。

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.zxj

zxj-spring-mvc

1.0-SNAPSHOT

zxj-spring-mvc

http://www.example.com

war

UTF-8

1.7

1.7

4.3.18.RELEASE

junit

junit

4.12

test

org.springframework

spring-test

${spring.version}

test

org.springframework

spring-context

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-aspects

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

javax.servlet.jsp.jstl

jstl-api

1.2

${project.artifactId}

maven-clean-plugin

3.1.0

maven-install-plugin

2.5.2

4、Project Structure的编辑,创建一下包名。

2016664019b3305505ef455469a6e0bf.png

二、webapp的编辑

1、目录结构。

46b5a2f07555b81e874309b5e3580c5f.png

2、web.xml。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

index.jsp

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

/WEB-INF/dispatcher-servlet.xml

1

dispatcher

/

3、applicationContent.xml。

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

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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.xsd">

4、dispatcher-servlet.xml。

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

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

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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.xsd

http://www.springframework.org/schema/mvc

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

5、index.jsp默认页面。

hello world

welcome zhuoxiaojie spring mvc

6、hello.jsp。

test

hello world

7、test2.jsp。

test

key1: ${key1} , key2: ${key2}

三、Controller层

package com.xiaojie.spring.mvc.controller;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;

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

import org.springframework.web.servlet.ModelAndView;

@Controller

@RequestMapping("/test")

public class TestController {

@RequestMapping("/hello")

public ModelAndView test1(Model model) {

return new ModelAndView("hello");

}

@GetMapping("/test2")

public ModelAndView test2(Model model) {

model.addAttribute("key1", "卓小杰");

model.addAttribute("key2", "你真帅");

return new ModelAndView("test2");

}

}

四、Tomcat的配置

1、下载Tomcat8。自己去百度教程下载。

658f37aed89f6ece72d859e2a542cc40.png

2、用Tomcat进行启动项目的配置。然后启动项目。

war模式:将web工程以war包的形式上传到服务器

war exploed模式:将web工程以当前文件夹的位置关系上传到服务器

1b00a56962ab3a3adcc4785b02416244.png

6d1f4218f1e82bcd7b633ecd5d50a499.png

五、测试结果

1、启动之后的默认界面index.jsp。

a49cecf65f73c129752372bf49159e56.png

2、hello.jsp界面。

3ede30ffa3837651aeb2d849394b54b3.png

3、test2.jsp界面。带参数。

80961cb568e8e6b7cc358724e8bc29e8.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值