springMVC入門項目demo

  1. 引入jar包html

  2. web.xml 配置 DispatcherServlet 及 springmvc.xml 的路徑java

 <!-- Spring MVC -->
  <servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:/spring-mvc*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

3. spring-mvc.xml 配置web

<!-- 註解掃描包 -->
	<context:component-scan base-package="com.*" />
	
	<!-- 配置視圖解析 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/views/" />
    	<property name="suffix" value=".jsp" />
	</bean>

4.  編寫 controller 類spring

package com.web;

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

@Controller
@RequestMapping(value = "/helloworld")
public class HelloWorldController{

	@RequestMapping(value="sayHello")
	public String  sayHello(Model model){
		model.addAttribute("hello", "helloworld");
		return "/modules/helloworld";
		
	} 
}

5. 訪問;spring-mvc

<a href="/springmvc-helloworld/helloworld/sayHello">sayHello</a>

6. 顯示:mvc

${hello}

7. 結果:app

源碼
jsp

相關文章
相關標籤/搜索