Spring-mvc入门案列

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <!-- 配置spring -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 如果applicationContext.xml不在web-info下则要配置 -->
    <context-param>
    <param-name>contextConfigLocation</param-name><!--contextConfigLocation名不能改 -->
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!-- 配置spring mvc -->
    <servlet>
        <servlet-name>qq</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 如果qq-servlet.xml放在web-info文件夹下则不用配置,contextConfigLocation名不能改 -->
        <init-param>
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>qq</servlet-name>
        <url-pattern>*.do</url-pattern><!-- 是spring mvc开发习惯要.do结尾,struts要用.action -->
    </servlet-mapping>
    <!-- spring mvc处理乱码 -->
    <filter>
    <filter-name>bm</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <!-- 设置编码参数 -->
    <init-param>
    <param-name>encoding</param-name><!--encoding不能变  -->
    <param-value>utf-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>bm</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:jdbc.properties" />

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="initialPoolSize" value="5"></property>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="10" />
        <property name="maxIdleTime" value="600" />
        <property name="maxStatements" value="0" />
    </bean>
    
    <bean id="userDaoImpl" class="com.hw.dao.impl.UserDaoImpl">
        <property name="dataSource">
        <ref bean="dataSource"/>
        </property>
    </bean>


</beans>

springmvc.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.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.xsd">

<!--注解映射器  -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!--注解适配器  -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<context:component-scan base-package="com.hw"></context:component-scan>
<!--使用mvc:annotation-driven上面的注解映射器和注解适配器将被替代  -->
<mvc:annotation-driven></mvc:annotation-driven>

    <!--视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/view/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

控制层(传接值)

package com.hw.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hw.dao.impl.UserDaoImpl;
import com.hw.domain.User;

@Controller
@RequestMapping("view")
public class UserController {

    UserDaoImpl db = new UserDaoImpl();

    @RequestMapping("hello")
    // 如果访问此访问:则路径为:user/hello.do
    public String hello() {
        return "hello";
    }

    @RequestMapping("toadd")
    // 如果访问此访问:则路径为:user/toadd.do
    public String toadd() {
        return "add";
    }

    public String add(Model model, User uu) {
        /*
         ****第1 种方法接值spring mvc , add(String name, float money, Date jobtime):
         * 只要方法的括号中属性我和表单中的的属性名一致则自动取值且类型会自动转换 System.out.println(name + " " + money +
         * " " + jobtime);
         * 
         * 第2 种方法接值spring mvc , add(User uu) 只要方法的括号里写上类属性即可(属性名和表单中的属性名一致)
         * System.out.println(uu.getName() + " " + uu.getMoney() + " " +
         * uu.getJobtime());
         * 
         * 第3 种方法接值spring mvc add(HttpServletRequest request) 只要在括号里写上HttpServletRequest
         * request,然后即可request.getParameter("name")取值
         * 
         * String name=request.getParameter("name"); float
         * money=Float.parseFloat(request.getParameter("money")); String
         * jobtime=request.getParameter("jobtime"); System.out.println(name + " " +money
         * + " " +jobtime);
         * 
         * 
         * spring mvc默认为转发方式,如改为重定向则是为:redirect:list.do
        
         ***传值:第 1种方法使用request.setAttribute转发有效,如果重定向有效则用session.setAttribute
         * HttpSession session = request.getSession();//创建session对象
         
        // request.setAttribute("name", name);
        // request.setAttribute("money", money);
        // return "list"; //转发
        // session.setAttribute("name", name);
        // session.setAttribute("money", money);
        /*
         * 传值 :第1种使用:request对象,对转发有效如重定向则要用session对象 
         *      第2种使用:Model传值时可以用使用,对转发有效,对重定向无效
         * 
         */

        model.addAttribute("name", uu.getName());
        model.addAttribute("money", uu.getMoney());
        model.addAttribute("uu", uu);
        return "list"; // 转发
        // return "redirect:list.do"; //重定向
    }
    @RequestMapping("addview")
    public ModelAndView addview(User uu){
    Map<String,Object> map=new HashMap<String,Object>();    
        map.put("name", uu.getName());
        map.put("money", uu.getMoney());
        map.put("uu", uu);
        return new ModelAndView("list",map);
        //第3种使用:ModelAndView传值时可以用使用,对转发有效,对重定向无效
        //return new ModelAndView("redirect:list.do",map);
        
    }
    @RequestMapping("list")
    // 如果访问此访问:则路径为:user/list.do
    public String list() {
        System.out.println("欢迎来看list页面!!!");
        return "list";
    }

    // spring mvc对时间格式处理,Date要导入java.util包下的
    @InitBinder
    private void InitBinder(ServletRequestDataBinder bind) {
        bind.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容