第一个springMVC入门程序
先看下项目结构
要加载与spring相关的包
HelloController.java(注意是在包controller下)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloController { @RequestMapping (value= "/welcome" ,method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute( "message" , "Spring 3 MVC Hello World" ); return "hello" ; } } |
hello.jsp(注意是放在WEB-INF下的pages目录下)
1 2 3 4 5 6 7 8 9 10 11 12 | <%@ page language= "java" contentType= "text/html; charset=ISO-8859-1" pageEncoding= "ISO-8859-1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=ISO-8859-1" > <title>Insert title here</title> </head> <body> <h1>Message : ${message}</h1> </body> </html> |
mvc-dispatcher-xml (注意:一般是放置在WEB-INF目录下,可以放置在src目录下)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <beans xmlns= "http://www.springframework.org/schema/beans" 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 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"> <context:component-scan base- package = "controller" /> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" > <value>/WEB-INF/pages/</value> </property> <property name= "suffix" > <value>.jsp</value> </property> </bean> </beans> |
web.xml(放置在WEB-INF 目录下)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <web-app id= "WebApp_ID" version= "2.4" xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http: //java.sun.com/xml/ns/j2ee http: //java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet- class > org.springframework.web.servlet.DispatcherServlet </servlet- class > <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> </context-param> <listener> <listener- class > org.springframework.web.context.ContextLoaderListener </listener- class > </listener> </web-app> |
index.jsp
============这个可以里面什么都不用写==========最后启动tomcat服务器,在url中输入 localhost:8080/spring/welcome
结果
作者: lost blog
出处: http://www.cnblogs.com/JAYIT/
关于作者:专注服务器端开发
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接 如有问题, 可邮件(sawyershaw@qq.com)咨询.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步