发表于: 2022-05-03 17:32:32

1 605



今天学习spring知识点,之后尝试再搭建一个spring mvc :


创建一个spring项目在java中添加

HelloController

添加代码:

package com.cjx.controller;


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


/**
* @author
* @date Created in 2019/12/1 11:11
* @description controller
*/
//声明控制台
@Controller
@RequestMapping("/Web-INF")
public class HelloController {

@RequestMapping("/jsp")
public String sayHello(){

System.out.println("再次尝试spring mvc成功!");
       return "success";
   }

}



在pom文件上添加依赖:

<?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>spring_mvc</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>

 <name>spring mvc 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.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
   <java.version>1.8</java.version>
   <spring.version>5.2.0.RELEASE</spring.version>
 </properties>

 <dependencies>
   <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
     <scope>test</scope>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>${spring.version}</version>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-web</artifactId>
     <version>${spring.version}</version>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>${spring.version}</version>
   </dependency>
   <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>servlet-api</artifactId>
     <version>2.5</version>
     <scope>provided</scope>
   </dependency>
   <dependency>
     <groupId>javax.servlet.jsp</groupId>
     <artifactId>jsp-api</artifactId>
     <version>2.2.1-b03</version>
     <scope>provided</scope>
   </dependency>
 </dependencies>

 <build>
   <finalName>spring mvc</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>


     <plugins>
       <plugin>
         <groupId>org.apache.tomcat.maven</groupId>
         <artifactId>tomcat7-maven-plugin</artifactId>
         <version>2.2</version>
         <configuration>
           <port>8080</port>
           <path>/</path>
         </configuration>
       </plugin>
     </plugins>

 </build>


</project>


在resources中创建springmvcxml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      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
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
   <!-- 开启注解扫描 -->
   <context:component-scan base-package="com.cjx.controller"/>


   <!--处理映射器-->
   <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>


   <!--配置spring_mvc的视图解析器-->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/WEB-INF/jsp/"/>
       <property name="suffix" value=".jsp"/>
   </bean>

   <!--
   自动注册最合适的处理器映射器,处理器适配器(调用handler方法)-->
   <mvc:annotation-driven/>

   <!--静态资源权限开放-->
   <mvc:default-servlet-handler/>



</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>

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



 </servlet>
 <servlet-mapping>
   <servlet-name>dispatcherServlet</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>


在WEB-INF文件当中创建一个success.jsp文件:

<%--
 Created by IntelliJ IDEA.
 User: 86166
 Date: 2022/5/3
 Time: 10:35
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>Title</title>
</head>
<body>

   <h1>入门程序</h1>
   <a href="hello">入门程序</a>

</body>
</html>




遇到错误:

 Some problems were encountered while processing the POMs: 

'artifactId' with value 'spring mvc' does


解决方法:POM文件为gav格式, 不允许中文字符或空格 ,仅使用下划线,数字,字母 将artifactId属性值改为符合要求的形式即可


遇到问题:网页上查询时出现404错误:

网上查询了很多资料还是没有解决



通过今天的学习也尝试,将很多之前第一次创建时候不懂的地方理解了。



明天目标:将今天没有解决的问题解决。学习spring AOP。




返回列表 返回列表
评论

    分享到