欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring MVC 框架搭建

程序员文章站 2022-05-17 09:28:16
...

基础spring MVC 框架搭建

JAR包
链接: https://pan.baidu.com/s/1eSCG0Tk 密码: k6n6
spring-framewokr - 4.2.0
common-logging-1.2-bin

(将jar包解压后放到WebContent/WEB-INF/lib目录下) 然后导入

首先创建 Dynamic Web Project  
在WEB-INF 目录下创建 web.xml  ;  springmvc-config.xml 


两个文件配置如下

web.xml

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-config.xml</param-value>
</init-param>

此处代码意思是 servlet控制文档的位置 需要根据自己需要更改


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.1"
 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_3_1.xsd">
 <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  	<init-param>
   		 <param-name>contextConfigLocation</param-name>
    	 <param-value>/WEB-INF/springmvc-config.xml</param-value>
    </init-param>
 </servlet>
 <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
 	<welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>


springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
  http://www.springframework.org/org/schema/mvc 
  http://www.springframework.org/org/schema/mvc/spring-mvc-4.2.xsd
  http://www.springframework.org/schema/context  
  http://www.springframework.org/schema/context/spring-context-4.2.xsd">  
     
	<context:component-scan base-package="org.fkit.controller"/>  
	<!-- 配置annotation类型的处理映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
	<!-- 配置annotation类型的处理器适配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
	<!-- 视图解析器  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>


--------------------------- 在WebContent目录下创建index.jsp 作为主页--------------------------- 

index.jsp


<%@ 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>
<a href = "hello.html">say hello</a>
</body>
</html>


在WEB-INF目录下创建目录content
在content下创建welcome.jsp


welcom.jsp

<%@ 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>
${$requestScope.message}
<a>hello world</a>
</body>
</html>


在src目录下创建org.fkit.controller包
在org.fkit.controller包下创建 class HelloController.java


 HelloController.java

package org.fkit.controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * HelloController是一个基于注解的控制器,
 * 可以同时处理多个请求动作,并且无需任何接口。
 * org.springframework.stereotype.Controller注解用于指示该类是一个控制器
 * @author hasee
 *
 */
@Controller

public class HelloController {
	private static final Log logger=LogFactory.getLog(HelloController.class);
	
	/**
	 *	org.springframework.web.bind.annotation.RequestMapping注解
	 *	用来映射请求URL和请求的方法等。本例用来映射“/hello”
	 *	hello只是一个普通方法。
	 *	该方法返回一个包含视图名火视图名和模型的ModelAndView对象
	 */
	@RequestMapping(value="/hello")
	public ModelAndView hello(){
		logger.info("hello 被调用");
		ModelAndView mv =new ModelAndView();
		mv.addObject("message","hello world");
		mv.setViewName("/WEB-INF/content/welcome.jsp");
		return mv;

}

}

spring MVC 框架搭建


运行结果

spring MVC 框架搭建


spring MVC 框架搭建


希望大家多指正交流





相关标签: spring mvc 框架