Spring MVC系列:(7)快速入門(註解版本)


一、引入jar包
html

spring-core

commons-logging-1.2.jarjava

spring-beans-3.2.5.RELEASE.jarweb

spring-context-3.2.5.RELEASE.jarspring

spring-core-3.2.5.RELEASE.jarexpress

spring-expression-3.2.5.RELEASE.jarspring-mvc

spring-web spring-web-3.2.5.RELEASE.jar
spring-webmvc spring-webmvc-3.2.5.RELEASE.jar


二、配置mvc

web.xmlapp

<?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" 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">
  <display-name>springmvc02</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 註冊SpringMVC核心控制器 -->
  <servlet>
  	<servlet-name>SpringMVC</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>
  </servlet>
  <servlet-mapping>
  	<servlet-name>SpringMVC</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <!-- 註冊Spring的編碼過濾器,位於spring-web的jar包中 -->
  <filter>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>UTF-8</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>


springmvc.xmljsp

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- <import resource="com/rk/config/spring-test.xml"/>     -->

    <!-- Action控制器 -->
    <context:component-scan base-package="com.rk.action"></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"/>

</beans>


三、建立HelloWorldAction控制器ide

HelloWorldAction.java

package com.rk.action;

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

@Controller
public class HelloWorldAction {
    @RequestMapping(value="/test")
	public String test(Model model) throws Exception{
	    System.out.println("HelloWorldAction.test()");
	    model.addAttribute("message", "這是個人第二個SpringMVC應用程序!");
	    return "/index.jsp";
	}

}


四、JSP頁面

修改WebRoot/下的index.jsp頁面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Index Page</title>
    </head>
  
    <body>
        This is my JSP page. <br>
        ${message }
    </body>
</html>


五、部署訪問 


訪問地址:

http://127.0.0.1:8080/springmvc02/test.action


wKioL1fgV7-RdToVAAA6Y-GUf9w724.png

相關文章
相關標籤/搜索