1.第一个springmvc实例helloword 关键点拨

1.1 web.xml文件的配置

<servlet>
  <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-servlet.xml</param-value>
  </init-param>
  <load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springMVC</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

说明:<url-pattern>/</url-pattern>表示拦截所有请求

1.2 spring-servlet文件的配置

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <bean name="/test1/helloworld" class="com.juin.controller.HelloWorldController" />

    <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="do"></property>
    </bean>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
 </beans>  

说明:配置参数名称解析器paramMethodResolver,spring自带的,我们注入即可。

  解析器是靠什么解析,是paramName配置了do,请求的时候靠这个来解析的。

1.3 新建HelloWorldController.java文件,内容如下:

package com.juin.controller;

public class HelloWorldController {

    String hello = "hello world";
    System.out.println(hello);

}

1.4 运行,在控制台里输出 hello world.

2.静态文件访问(图片,css,js等)

2.1 以图片为例,在1.2的spring-servlet.xml文件中添加

<bean name="/test1/img" class="com.juin.controller.StaticFileController">
        <property name="methodNameResolver">
            <ref bean="paramMethodResolver"/>
        </property>
</bean>

  <!-- 静态文件访问 -->
  <mvc:resources location="/img/" mapping="/img/**"/>
  <mvc:resources location="/js/" mapping="/js/**"/>
  <mvc:resources location="/css/" mapping="/css/**"/>

2.2 创建图片访问类StaticFileController 和 用来显示图片的staticFile.jsp页面

public class StaticFileController extends MultiActionController{
    public ModelAndView img(HttpServletRequest request,HttpServletResponse response){
        return new ModelAndView("/staticFile");
    }

}

3.启用注解和注解优化

3.1启用注解

3.1.1 建立类UserController.java,代码如下:

 package com.juin.annotation;

 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;

 @Controller
 public class UserController {

         //注解
         @RequestMapping(value="/user/addUser",method=RequestMethod.POST)
         public ModelAndView addUser(){
             String result="this is addUser method";
             return new ModelAndView("/annotation","result",result);
         }
         @RequestMapping(value="/user/delUser",method=RequestMethod.GET)
         public ModelAndView delUser(){
             String result="this is delUser method";
             return new ModelAndView("/annotation","result",result);
         }
         @RequestMapping(value="/user/toUser",method=RequestMethod.GET)
         public ModelAndView toUser(){
             return new ModelAndView("/annotation");
         }
 }

说明:@Controller是对类的注解,@RequestMapping是对类中方法的注解。

对应的annotion.jsp的代码为:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/SpringMVC02/user/addUser" method="post">
        <div>
            <h>注解参数传递</h><br/>
            ${result }<br/>

            <input type="submit" value="post提交">

        </div>
    </form>
</body>
</html>

3.1.2 替换spring-servlet配置文件

 <?xml version="1.0" encoding="UTF-8"?>
 <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">

     <!-- 扫描指定包下的所有类 -->
     <context:component-scan base-package="com.juin.annotation"/>
     <!-- 启用注解的两个bean -->
     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

     <!-- 静态文件访问 -->
     <mvc:resources location="/img/" mapping="/img/**"/>
     <mvc:resources location="/js/" mapping="/js/**"/>
     <mvc:resources location="/css/" mapping="/css/**"/>

     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix" value="/"></property>
         <property name="suffix" value=".jsp"></property>
     </bean>
  </beans>  

3.2 注解优化

3.2.1 用<mvc:annotation-driven/>替换

  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
  <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

3.2.2 优化UserController.java中的代码

 package com.juin.annotation;

 import javax.servlet.http.HttpServletRequest;

 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;

 @Controller
 @RequestMapping("/user")
 public class UserController {

         //注解
         @RequestMapping("/addUser")
         public String addUser(HttpServletRequest request){
             String result="this is addUser method";
             request.setAttribute("result", result);
             return "/annotation";
         }
         @RequestMapping("/delUser")
         public String delUser(HttpServletRequest request){
             String result="this is delUser method";
             request.setAttribute("result", result);
             return "/annotation";
         }
         @RequestMapping("/toUser")
         public String toUser(){
             return "/annotation";
         }
 }

4.springmvc参数传递

4.1 创建addUser.jsp表单提交页面,用于提交用户的姓名和年龄

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>login</title>

 <script type="text/javascript">
     function addUser(){
         var form = document.forms[0];
         form.action = "/SpringMVC4/user/data/addUser";
         form.method="get";
         form.submit();
         }
 </script>

 </head>
 <body>
     <form action="">
         <div>
             <input type="text" name = "userName"/>
              <input type="text" name = "age"/>
             <input type="button" value="提交" onclick="addUser()"/>
         </div>
     </form>
 </body>
 </html>

4.2 创建 User的实体类

 package com.juin.entity;

 public class User {
     private String userName;
     private String age;

     public String getUserName() {
         return userName;
     }

     public void setUserName(String userName) {
         this.userName = userName;
     }

     public String getAge() {
         return age;
     }

     public void setAge(String age) {
         this.age = age;
     }
 }

4.3创建类DataController.java类

 package com.juin.annotation;

 import javax.servlet.http.HttpServletRequest;

 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;

 import com.juin.entity.User;

 @Controller
 @RequestMapping("/user/data")
 public class DataController {

         //注解
         @RequestMapping("/addUser")
         public String addUser(User user,HttpServletRequest request){
 //            String result="this is addUser method";
 //            request.setAttribute("result", result);
             request.setAttribute("userName", user.getUserName());
             request.setAttribute("age", user.getAge());
             return "/userManage";
         }
         @RequestMapping("/delUser")
         public String delUser(HttpServletRequest request){
             String result="this is delUser method";
             request.setAttribute("result", result);
             return "/annotation";
         }
         @RequestMapping("/toUser")
         public String toUser(){
             return "/addUser";
         }
 }

springmvc学习路线1-基本配置的更多相关文章

  1. springMVC学习路线3-整合spring(annotion方式)

    个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助.不过,如果用都不会,谈思想就变成纸上谈兵了!!!先技术,再思想.实践出真知. 1.基本概念 1.1 Spring   Sp ...

  2. 【转】selenium学习路线

    selenium学习路线 配置你的测试环境,真对你所学习语言,来配置你相应的selenium 测试环境.selenium 好比定义的语义---“问好”,假如你使用的是中文,为了表术问好,你的写法是“你 ...

  3. SpringMVC 学习 十 SSM环境搭建(三)springMVC文件配置

    SpringMVC文件配置的详细过程,可以查看springMVC环境搭建的注解配置篇<springMVC学习三 注解开发环境搭建> <?xml version="1.0&q ...

  4. springMVC学习记录2-使用注解配置

    前面说了一下使用xml配置springmvc,下面再说说注解配置.项目如下: 业务很简单,主页和输入用户名和密码进行登陆的页面. 看一下springmvc的配置文件: <?xml version ...

  5. springMVC学习记录1-使用XML进行配置

    SpringMVC是整个spring中的一个很小的组成,准确的说他是spring WEB这个模块的下一个子模块,Spring WEB中除了有springMVC还有struts2,webWork等MVC ...

  6. springmvc学习指南 之---第25篇 Spring Bean有三种配置方式

    writed by不要张艳涛, 从tomcat转到了springmvc 现在开始有点不知道该看什么书了,看完了springmvc 学习指南之后 又查了一些书,好多都是内容相近,在找书的过程之中,发现s ...

  7. 一位资深程序员大牛给予Java初学者的学习路线建议

    java学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是我你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈 ...

  8. 一位资深程序员给予Java初学者的学习路线建议

    一位资深程序员给予Java初学者的学习路线建议 java学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是我你是如何学习Java的,能不能给点建议?今天我是打算来点干 ...

  9. 我推荐的 Java Web 学习路线

    晚上再 V2 的 Java 的节点看到有人问 Java Web 书籍推荐.我这半年多的时间,也从别的方向开始转向 Java 服务端开发,所以,我来说下我的学习路线,帮助有需要的朋友把半只脚踏进 Spr ...

随机推荐

  1. SpringBoot------自定义拦截器

    1.添加pom.xml使用的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt ...

  2. linux Ubuntu系统安装百度aip

    1.下载百度api pip install baidu-aip 2.配置视频转码工具ffmpeg Ubuntu16.04下安装FFmpeg(超简单版) 第一步:添加源. sudo add-apt-re ...

  3. poj3723_Conscription

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12393   Accepted: 4350 Des ...

  4. hibernate09--连接查询

    创建实体类 package cn.bdqn.bean; import java.util.ArrayList; import java.util.Date; import java.util.List ...

  5. MultipartFile实现文件上传

    一.主要有两个java类,和一般的servlet放在一起即可. 1.FileUploadBean.java package chb.demo.web;import org.springframewor ...

  6. mysql拿webshell总结

    1.select '<?php eval($_POST[jumbo]) ?>' into outfile '/var/www/jumbo.php'; 2.select '<?php ...

  7. Oracle课程档案,第七天

    数据库管理 关闭数据库有4中方式: 1.shutdown modes 关机模式 2.shutdown normal 关机正常 3.shutdown immediate 立即关闭 ★★ 常用命令 4.s ...

  8. myEclipse导入现成项目出现错误 【申明来源于网络】

    myEclipse导入现成项目出现错误 [申明来源于网络] 原地址:http://blog.sina.com.cn/s/blog_6d7703400100znh6.html file–>impo ...

  9. Java新帮派——数组

    一.什么是数组: 数组是一个变量,存储相同数据类型的一组数据 声明一个变量就是在内存空间划出一块合适的空间 声明一个数组就是在内存空间划出一串连续的空间 二.数组基本要素: 标识符:数组的名称,用于区 ...

  10. MySQL优化小结

    数据库的配置是基础.SQL优化最重要(贯穿始终,每日必做),由图可知,越往上优化的面越小,最基本的SQL优化是最重要的,往上各个参数也没太多调的,也不可能说调一个innodb参数性能就会好多少,而动不 ...