HelloSpringMVC注解版实现步骤解析

网友投稿 235 2022-11-24


HelloSpringMVC注解版实现步骤解析

注解版步骤

新建一个module,添加web的支持

由于Maven可能存在资源过滤的问题,我们将配置完善pom.xml

src/main/java

**/*.properties

**/*.xml

false

src/main/resources

**/*.properties

**/*.xml

false

在pomhttp://.xml文件引入相关的依赖

主要有Spring框架核心库、SpringMVC、servlet、jsTL等,我们在父依赖中已经引入了!

配置web.xml

注意点:

注意web.xml版本问题,要最新版;

注册DispatcherServlet

关联SpringMVC的配置文件

启动级别为1

映射路径为/【不要用/*】

配置springmvc配置文件和视图解析器

我们把所有视图都存放在/WEB-INF/目录下,可以保证视图安全,因为这个目录下的文件,客户端不能直接访问。

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http:// https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

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

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http:// https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

https://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc

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

创建Controller

@Controller

@RequestMapping("/hello")

public class HelloController {

//真实访问地址:项目名/hello/h1

@RequestMapping("/h1")

public String Hello(Model model){

model.addAttribute("msg","Hello SpringMVC annotation!");

return "hello"; //会被视图解析器处理

}

}

创建视图层

视图可以直接取出并展示从Controller带回的信息,可以通过EL表示取出Model中存放的值或者对象;

配置Tomcat运行

小结

实现步骤其实很简单:

新建一个web项目

导入相关jar包

编写web.xml,注册DispatcherServlet

编写springmvc配置文件

创建对应的控制类,controller

完善前端视图和controller之前的对应

配置tomcat,测试运行调试。

springMVC必须配置的三大件:

处理器映射器

处理器适配器

视图解析器

通常只需要手动配置视图解析器,而处理器映射器和处理器适配器只需要开启注解驱动即可,省去了大段的xml配置。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:一篇文章解决Java异常处理
下一篇:SpringBoot 如何实现Session共享
相关文章

 发表评论

暂时没有评论,来抢沙发吧~