java搭建spring mvc项目

java搭建spring mvc项目

1:下载jar包

下载spring jar包:点我下载 提取码: bh8r

2:配置web.xml

首先观察目录结构
在这里插入图片描述
配置web.xml文件


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         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">

	<servlet>
	  <servlet-name>mvc</servlet-name>
	  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	  <servlet-name>mvc</servlet-name>
	  <url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

问题答案
DispatcherServlet请求驱动都围绕这个控制器开分派请求
load-on-startup标签等于0时,Web容器启动时就加载并初始化此Servlet,数字越小,被创建的越早。当值小于0或不指定时,Servlet在真正被使用时才被创建。值相同时,容器自己选择次序
url-pattern对静态文件,css、js拦截
servlet-name默认情况下,应用会去WEB-INF文件夹下查找对应的[name]-servlet.xml文件
init-param指定配置文件路径

在这里插入图片描述

3:HelloWorldMVC.java

@Controller
public class HelloWorldMVC{
		@RequestMapping("/test")
	   public @ResponseBody String hello (){
	       System.out.println("Hello World!");
	       return "hello";   
	   }
	   @RequestMapping("/test2")
	   public ModelAndView upload(){
	       System.out.println("世界你好");
	       return null;
	   }
}
  1. @Controller:表明此类的实例是一个控制器,我们请求会在Controller中匹配方法进行处理.
  2. @RequestMapping:方法会被分发处理器扫描识别,将不同的请求分发到对应的接口上。将@Controller标记在某个类上,配合@RequestMapping注解,可以在一个类中定义多个接口,这样使用起来更加灵活。

4:mvc-servlet.xml

<?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"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-3.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
      ">
   
       <!-- 启动springMVC注解 -->
	   <mvc:annotation-driven/>
	   <!-- 扫描注解所在的包 -->
	   <context:component-scan base-package="javatest"></context:component-scan>
    
</beans>
  1. mvc:annotation-driven/:配置用来启动Spring MVC的注解。此配置会自动注册为RequestMappingHandlerMapping和RequestMappingHandlerAdapter两个Bean。
  2. <context:component-scan base-package="">:在xml配置了这个标签后,Spring可以自动去扫描base-pack下面或者子包下面的java文件,如果可以扫描到含有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。

5:运行

在这里插入图片描述
在这里插入图片描述

6:跳转到某个页面

这是返回一个字符串,如果想跳转到某个页面呢?

<?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"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-3.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
      ">
   
        <!-- 启动springMVC注解 -->
	    <mvc:annotation-driven/>
	    <!-- 扫描注解所在的包 -->
	    <context:component-scan base-package="javatest"></context:component-scan>
	   
	      <!-- 配置视图解析器 -->
	      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
		        <!-- 前缀 -->
		        <property name="prefix" value="/WEB-INF/" />
		        <!-- 后缀 -->
		        <property name="suffix" value=".jsp" />
		   </bean>
    
</beans>
  • property name=“prefix” value="/WEB-INF/" /:在根目录/WEB-INF/下查找相关文件;
  • WEB-INF:WEB-INF是Java的WEB应用的安全目录。所谓安全就是客户端无法访问,只有服务端可以访问的目录。
    如果想在页面中直接访问其中的文件,必须通过web.xml文件对要访问的文件进行相应映射才能访问。
  • property name=“suffix” value=".jsp" /:拼接返回的值

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
搭建Spring MVC项目时,您可以按照以下步骤进行操作: 1. 创建一个Maven项目,并添加相关的依赖。您可以在pom.xml文件中添加Spring MVC的相关依赖,例如: ```xml <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <!-- 其他依赖 --> </dependencies> ``` 2. 创建一个`Spring MVC`的配置文件(例如springmvc.xml),并在配置文件中进行相关的配置。您可以在配置文件中指定`<context:component-scan>`来扫描`Controller`类,并将其注册为Bean,例如: ```xml <context:component-scan base-package="cn.com.buba.controller" /> ``` 3. 创建一个`Controller`类,并在类上使用`@Controller`注解进行标识。您可以在类上使用`@RequestMapping`注解来指定URL映射,例如: ```java @Controller @RequestMapping("/buba") public class FirstController { @RequestMapping("/firstController.do") public String firstController(){ System.out.println("this is firstController"); return "/first.jsp"; } } ``` 4. 配置`web.xml`文件,将`DispatcherServlet`配置为前端控制器。您可以在`web.xml`中添加以下配置: ```xml <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:springmvc.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> ``` 以上是搭建Spring MVC项目的基本步骤。根据您的需求,您还可以添加其他配置和功能来完善项目。如果遇到报错,您可以根据错误提示进行排查,也可以检查是否添加了必要的依赖和配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一拖再拖 一拖再拖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值