违规?
竟然违规?
沙雕csdn竟然说违规?
我把他当记事本难道有问题吗?
java
package com.hahah.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller//创建控制器,创建之后去xml文件中开启组件扫描,开启之后就交给ioc容器管理了
public class HelloController {
// 控制器里的方法是处理请求的方法
//控制器里可以有多个方法
@RequestMapping(value = "/")//请求映射注解,当前请求为斜线的时候,就会执行注解的方法
// 注解的方法就会返回视图名称
//将当前的请求和控制器方法,来创建映射关系
public String index(){//这个方法就是处理‘/’个请求的方法,方法名不起作用,起作用的是,请求路径与requestmapping有关
//返回视图名称,视图名称决定最终跳转到的界面
//html无法直接访问
//返回一个视图名称,就会被在springMVC.xml配置文件中配置的视图解析器,解析
//视图解析器前缀加上后缀就是要访问的界面(前缀就是/web-inf下面的路径)
//把前缀去掉后缀去掉就是视图名称(后缀就是文件类型)
return "index";
}
@RequestMapping("/target")//匹配到有这个请求的,会调用这个方法
public String totarget(){
return "target";//视图名称会被视图解析器解析
}
}
springmvc的配置文件
<?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:context="http://www.springframework.org/schema/context"
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">
<!-- 开启组件扫描,先要配置名称空间-->
<context:component-scan base-package="com.hahah.mvc"></context:component-scan>
<!-- <!– 配置Thymeleaf视图解析器 –> 如果使用jsp技术也要配置解析器-->
<!-- 专门解析Thymeleaf视图-->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<!-- 第一个参数设施视图解析的优先级–>-->
<property name="order" value="1"/>
<!--解析视图用到的编码-->
<property name="characterEncoding" value="UTF-8"/>
<!--当前的模板-->
<property name="templateEngine">
<!-- 内部bean,为templateEngine赋值-->
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<!-- 内部bean-->
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/template/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<!-- thymeleaf进行解析的时候,必须要设置视图前缀与试图后缀-->
<!-- 把当前视图的名称,加上前缀和后缀,就可以跳转到,指定的页面-->
<property name="templateMode" value="HTML5"/>
<!-- 页面的编码-->
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
</beans>
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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">
<!-- 配置SpringMVC的前端控制器,对浏览器发送的请求进行统一处理-->
<servlet>
<servlet-name>SpringMVC</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:将前端控制器DispatcherServlet的初始化时间提前到服务器启动时-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>