Spring MVC 简单实例

1.web.xml配置
web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-servlet.xml</param-value>
  </context-param>
 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- 对像js,css,gif等静态文件的访问,需要映射到默认的servlet -->
 <!-- 这里省去对静态资源url的配置,当然这样的话tomcat控制台就会报警告了,对html的请求、页面中的图片及css效果也是无法访问的 -->
 <!-- 配置spring核心servlet -->
 <servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
  <!-- url配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 -->
 </servlet-mapping>
</web-app>

2.Spring应用上下文配置,

在web.xml中我们配置了

spring的servlet

<servlet-name>spring</servlet-name>

web.xml里有这一段

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>

用于开启基于注解的springMVC功能那么我们需要在跟web.xml同一目录下新建一个spring-servlet.xml文件,内容为:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->
     <mvc:annotation-driven />
     <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
     <context:component-scan base-package="com.mvc.rest" />
     <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" />
</beans>

3.建包
在com.mvc.rest包下新建一个java类,代码如下:

package com.mvc.rest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RestConstroller {

    public RestConstroller() {
    }

    @RequestMapping(value = "/login/{user}", method = RequestMethod.GET)
    public ModelAndView myMethod(HttpServletRequest request,
            HttpServletResponse response, @PathVariable("user") String user,
            ModelMap modelMap) throws Exception {

        modelMap.put("loginUser", user);
        return new ModelAndView("/login/hello", modelMap);
    }

    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String registPost() {
        return "/welcome";
    }
}

3.建视图

正如spring-servlet.xml里

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" />

所配置的视图路径,我们需要在WEB-INF下建立view目录,在view下建立jsp文件;

正如RestConstroller里两个方法的返回语句:

return newModelAndView("/login/hello", modelMap);

return"/welcome";

所约定,我们需要在view下建立相应的login目录并在其下建立hello.jsp,在/WEB-INF/view下建立welcome.jsp
hello.jsp内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'hello.jsp' starting page</title>

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
  </head>

  <body>
    你好:<%=request.getAttribute("loginUser") %>,现在时间是<%= new Date() %>
  </body>
</html>

welcome.jsp内容随意
打开浏览地址栏输入:http://localhost:8080/springMVCDemo/login/abcd 这里的abcd是自定义的,作为请求参数解析;

输入:http://localhost:8080/springMVCDemo/welcome 则显示你welcome.jsp应该显示的内容。

P.S. springMVCDemo 为项目名

整个过程已经完成,需要注意的有几方面,

1.环境一定要是可用的

2.jar包正确的引入到项目中

3.配置文件要小心修改,不要犯括号不配对类似的低级错误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值