当前位置: 首页 > news >正文

javaee springMVC的简单使用 jsp页面在webapp和web-inf目录下的区别

项目结构

在这里插入图片描述

依赖文件

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>TestSpringMvc</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>TestSpringMvc Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- 导入SpringMvc 需要的jar包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.18.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.18.RELEASE</version></dependency><!-- 配置servlet--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency></dependencies><build><finalName>TestSpringMvc</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>

spring配置文件

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 1. 配置  需要扫描的控制层在哪个包  --><context:component-scan base-package="com.test.controller"></context:component-scan><!-- 2 配置 视图解析器 中的 前缀和后缀  --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 设置前缀  --><property name="prefix" value="/WEB-INF/"/><!-- 设置后缀 --><property name="suffix" value=".jsp"/></bean></beans>

web.xml

<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><!-- 加入前端控制器 DispatcherServlet --><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 设置 DispatcherServlet 的参数 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><!-- 1 启动服务器的时候就加载  --><!-- 0 使用的时候再加载 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2><a href="users/reg">注册</a></body>
</html>

UserController

package com.test.controller;import com.test.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;import javax.servlet.http.HttpServletRequest;@Controller
@RequestMapping("/users")
public class UsersController {@RequestMapping("/reg")public String reg(){//转发到指定的页面// 加上前缀和后缀 组成的完整路径// WEB-INF/reg.jspreturn "reg";}
}

reg.jsp

<%--Created by IntelliJ IDEA.User: HIAPADDate: 2019/11/28Time: 12:35To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
注册页面
</body>
</html>

知识点

放在webapp目录下的jsp页面可以直接通过URL访问到,放在web-inf目录下的页面,只能通过程序的转发或者重定向访问。

http://www.lryc.cn/news/161337.html

相关文章:

  • Docker容器技术实战-1
  • LeetCode算法题:2. 两数相加
  • ResNet 09
  • 什么是脚本语言,解释脚本语言的特点和应用领域
  • selenium 定位不到元素的几种情况
  • IDEA启动项目很慢,无访问
  • 时序预测 | MATLAB实现TCN-GRU时间卷积门控循环单元时间序列预测
  • 简单了解ARP协议
  • 【Linux】Stratis是什么?Stratis和LVM有什么关系和区别?
  • 植物大战僵尸修改金币【Steam下版本可行-其他版本未知】
  • GIS:生成Shp文件
  • 【日常笔记】使用Server过程中可能遇到的一些问题
  • 【Mysql】给查询记录增加序列号方法
  • Linux 安装elasticsearch-7.5.1
  • ElementUI浅尝辄止26:Notification 通知
  • IDEA新建的Moudle失效显示为灰色
  • Protobuf的简单使用
  • OpenCV 12(图像直方图)
  • LeetCode 面试题 03.06. 动物收容所
  • 快速理解DDD领域驱动设计架构思想-基础篇 | 京东物流技术团队
  • C++学习笔记(堆栈、指针、命名空间、编译步骤)
  • Rust Yew应用开发的事件初探
  • 高并发下单例线程安全
  • 【EKF】EKF原理
  • 蓝桥杯官网填空题(古堡算式)
  • Python---集合set
  • LORA项目源码解读
  • Azure + React + ASP.NET Core 项目笔记一:项目环境搭建(一)
  • html 学习 之 文本标签
  • 联发科3纳米芯片预计2024年量产,此前称仍未获批给华为供货