spring mvc 環境搭建

一.下載spring所需包

  可以從官網下載也可以從網上下載,

  主要是需要以下包即可

 com.springsource.org.aopalliance-1.0.0.jar

 com.springsource.org.apache.commons.logging-1.1.1.jar

 commons-logging-1.2.jar

 spring-aop-3.2.2.jar

 spring-aspects-3.2.2.jar

 spring-beans-3.2.2.jar

 spring-context-3.2.2.jar

 spring-context-support-3.2.2.jar

 spring-core-3.2.2.jar

 spring-expression-3.2.2.jar

 spring-web-3.2.2.jar

 spring-webmvc-3.2.2.jar

二.創建動態web工程,導入所需包,

三.代碼

 web.xml

<
?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
  <servlet>
<servlet-name>springDispatcherServlet</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>1</load-on-startup>
</servlet> 
 
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

springmvc.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
    ">
    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.springmvc"></context:component-scan>
    
    <!-- <bean  
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
   <bean  
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    
    <property name="prefix" value="/WEB-INF/views/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean>	
</beans>

java

package com.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Hello {
	  
		@RequestMapping("/hello")
		public String hello(){
			
			System.out.print("hello world");
			
			return "success";
		}
}

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">hello</a>
</body>
</html>

success.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>
hello hello
</body>
</html>

部署直接訪問  點擊hello即可

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章