SpringMVC学习笔记(二):基于注解的方式

来源:互联网 发布:如何下载别人网站源码 编辑:程序博客网 时间:2024/06/05 01:13

1、 创建工程,拷贝springmvc相关的jar,放入lib中
2、配置web.xml,配置前段控制器DispatcherServlet

<servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>

3、在web-inf下创建springmvc.xml, [servletname]-servlet.xml
文件名必须为springmvc-servlet.xml
4、配置HandlerMapping 根据beanname找到对应的controller(可以省略)
5、创建需要发出请求的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">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    <form action="hello.do" method="post">        hello:<input type="text" name="userName"/>        <input type="submit" value="submit">    </form>  </body></html>

6、创建返回界面。
index.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 'index.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>     <h1>${helloworld }</h1>  </body></html>

===========================================
前几个步骤与基于配置方式的是一样的。
7、创建一个Cotroller使用注解的方式配置(关键步骤)

package com.lin.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controller//声明为一个控制器public class HelloController {    @RequestMapping(value="/hello.do")//请求的映射,表示处理什么请求//下列方法是可以随便命名了,注意userName是与前端传递的参数的name一致    public String hello(String userName, Model model)    {        System.out.println(userName);        //向index.jsp界面回数据“helloworld"        model.addAttribute("helloworld", "hello" + userName);        return "index";    }}

8、编写springmvc-servlet.xml,由于使用注解的方式所以配置有所不同。不需要再配置controller,但是需要添加扫描器,与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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">    <!-- mvc 注解驱动 -->    <mvc:annotation-driven/>    <!-- 扫描器 -->    <context:component-scan base-package="com"></context:component-scan>    <!-- 配置视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!-- 配置前缀和后缀 -->        <property name="prefix" value="/"></property>        <property name="suffix" value=".jsp"></property>    </bean></beans>

9、至于最后的乱码处理,添加一个EncodeFilter编码过滤器,这里就不多讲了。

阅读全文
0 0