1. 程式人生 > >Spring MVC入門實例

Spring MVC入門實例

not 入門實例 mod 註解 ner art adding pac eclipse

1.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>
myweb</display-name>
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 載入全部的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-*.xml</param-value>
</context-param> <!-- 配置Spring監聽 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置字符集 --> <filter> <filter-name>encodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <!-- 配置SpringMVC --> <servlet> <description>myweb</description> <display-name>myweb</display-name> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

2.spring-mvc配置

<?

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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 開啟Spring MVC註解 --> <mvc:annotation-driven /> <!-- 處理靜態資源 --> <mvc:resources location="/resources" mapping="/resources/**" /> <!-- 打開Spring的Annotation支持 --> <context:annotation-config /> <!-- 註解掃描包 --> <context:component-scan base-package="com.myweb.controller" /> <!-- ViewResolver --> <!-- 默認的視圖解析器 在上邊的解析錯誤時使用 (默認使用html)- --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>

3.HelloController類

package com.myweb.controller; import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView; @Controllerpublic class HelloController {     @RequestMapping(value = "/hello", method = RequestMethod.GET)    public ModelAndView printWelcome() {        ModelAndView mv = new ModelAndView();        mv.setViewName("welcome");        mv.addObject("message", "you are welcome");        return mv;    } }


4.welcome.jsp


<%@ page language="java" contentType="text/html; UTF-8"    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; UTF-8"><title>Insert title here</title></head><body>${message}</body></html>

5.eclipse的文件夾結構圖

技術分享
技術分享

6.訪問http://localhost:8080/myweb/hello。輸出:

you are welcome
技術分享


Spring MVC入門實例