码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC学习 - 新建工程

时间:2015-02-10 13:09:07      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

 1.新建工程,当然是新建Dynamic Web Project。工程结构如下图所示:

技术分享

2.引入jar包,把spring-framework-3.2.5.RELEASE\libs\下除了源码和文档的包都引入,另外还要引入Apache的commons-logging包。

技术分享 

3.写修改web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6     <display-name>SpringMVC001</display-name>
 7     <servlet>
8         <servlet-name>springMvc</servlet-name>
9         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
10         <load-on-startup>1</load-on-startup>
11     </servlet>
12 
13     <servlet-mapping>
14         <servlet-name>springMvc</servlet-name>
15         <url-pattern>/</url-pattern>
16     </servlet-mapping>
17 </web-app> 

 4.写springMvc配置文件,因为servlet-name为springMvc所以配置文件命名为:springMvc-servlet.xml。配置内容如下所示:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context.xsd">
11     <context:annotation-config />
12     <context:component-scan base-package="com.jiutian.learning.mvc.buniness.controller"/>
13     <bean id="viewResolver"
14           class="org.springframework.web.servlet.view.InternalResourceViewResolver"
15           p:prefix="/jsp/"
16           p:suffix=".jsp" />
17 </beans> 

 5.写controller

 1 package com.jiutian.learning.mvc.buniness.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 
 7 @Controller
 8 @RequestMapping(value="/hello")
 9 public class HelloController {
10 
11     @RequestMapping(value="welcome",method = RequestMethod.GET)
12     public String hello(){
13         return "welcome";
14     }
15     
16 }

 6.写jsp,因为p:prefix="/jsp/" p:suffix=".jsp" 所以要建立如下图的结构:

技术分享 

 welcome.jsp内容如下所示:

 1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 6 <title>Insert title here</title>
 7 </head>
 8 <body>
 9     <h1>hello  welcome</h1>
10 </body>
11 </html>

 7.部署程序,并访问:http://localhost:8080/mvc/hello/welcome,效果如下图示:

技术分享 

 

 

springMVC学习 - 新建工程

标签:

原文地址:http://www.cnblogs.com/localhost8888/p/4283339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!