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

Spring MVC体系结构(五)

时间:2018-06-21 22:31:00      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:har   lte   自动填充   att   参数   ati   配置   tco   show   

一、spring mvc框架搭建步骤

  1、导入jar文件

    spring-web-3.2.0.RELEASE

    spring-webmvc-3.2.0.RELEASE

  2、在web.xml中配置servlet和其他参数

<?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">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 在web.xml中配置前端控制器,DispatcherServlet是spring最和核心的类 -->
  <servlet>
          <servlet-name>DispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:applicationContext.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
          <servlet-name>DispatcherServlet</servlet-name>
          <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 通过过滤器设置字符编码 -->
  <filter>
          <filter-name>CharacterEncodingFilter</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          <init-param>
              <param-name>encoding</param-name>
              <param-value>utf-8</param-value>
          </init-param>
  </filter>
  <filter-mapping>
          <filter-name>CharacterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

  3、配置applicationContext.xml 

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    
    <context:component-scan base-package="com.action"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
</beans>

  4、创建controller--->>处理请求的控制器

package com.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.entity.User;

@Controller
public class UserAction {
    
    @RequestMapping("submit")
    public ModelAndView show(User user){
        ModelAndView m = new ModelAndView("list");
        m.addObject("uname",user.getUname());
        m.addObject("upass",user.getUpass());
        return m;
    }
}

 

 二、controller控制器中参数传递的几种方式

  1、可以通过已经创建的javabean注入

import com.entity.User;

@Controller
public class UserAction {
    
    @RequestMapping("submit")
    public ModelAndView show(User user){
        ModelAndView m = new ModelAndView("list");
        m.addObject("uname",user.getUname());
        m.addObject("upass",user.getUpass());
        return m;
    }
}

  2、对应名称自动填充

@RequestMapping("submit")
    public ModelAndView show(String uname,String upass,HttpServletRequest request){
        ModelAndView m = new ModelAndView("list");
        m.addObject("uname",uname);//需要和前台的name名称一致
        m.addObject("upass",upass);//需要和前台的name名称一致
        return m;
    }

  3、名称不一致的情况需要添加注解描述

@RequestMapping("submit")
    /**
     * 针对前台name的名称与参数名称不一致的情况下使用
     * value  参数名
     * required  是否必须
     * defaultValue  如果为空,就当前默认值
     */
    public ModelAndView show(@RequestParam(value="uname",required=false,defaultValue="xxx") String uname,String upass){
        ModelAndView m = new ModelAndView("list");
        m.addObject("uname",uname);//需要和前台的name名称一致
        m.addObject("upass",upass);//需要和前台的name名称一致
        return m;
    }

 

Spring MVC体系结构(五)

标签:har   lte   自动填充   att   参数   ati   配置   tco   show   

原文地址:https://www.cnblogs.com/newbest/p/9210846.html

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