Java 系列之spring学习--springmvc搭建(四)
一、建立java web 项目
二、添加jar包
spring jar包下载地址http://repo.spring.io/release/org/springframework/spring/
2.1、导入jar包
先不管哪些包的具体作用,直接全部导入。
三、配置web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version= "1.0" encoding= "UTF-8" ?> <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 所有的请求都会被dispatherservlet捕获 --> <servlet> <servlet-name>springmvc</servlet-name> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class > <load- on -startup>1</load- on -startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> |
四、新建一个控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package springmvc; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class welcomeController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { // TODO Auto-generated method stub System.out.print( "hello springmvc" ); return null ; } } |
五、建立springmvc-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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:tx= "http://www.springframework.org/schema/tx" xmlns:context= "http://www.springframework.org/schema/context" 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.2.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-3.2.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.2.xsd http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" ></bean> <bean name= "/welcome.html" class = "springmvc.welcomeController" > </bean> <!-- 视图解释类 --> <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/WEB-INF/JSP/" /> <property name= "suffix" value= ".jsp" /> </bean> </beans> |
六、Look
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步