• Hello World


    controller

    public class HelloWorldController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
    //1、收集参数、验证参数
    //2、绑定参数到命令对象
    //3、将命令对象传入业务对象进行业务处理
    //4、选择下一个页面
    ModelAndView mv = new ModelAndView();
    //添加模型数据 可以是任意的POJO对象
    mv.addObject("message", "Hello World!!!!");
    //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
    mv.setViewName("hello");
    return mv;
    }
    }

    jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

    Hello World ${message} message=<%=request.getParameter("message")%>

    web.xml

    Archetype Created Web Application mymvct org.springframework.web.servlet.DispatcherServlet 1 mymvct /

    mymvct-servlet.xml

    <!-- HandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    
    <!-- HandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    
    <!-- ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 处理器 -->
    <bean name="/hello" class="cn.myspringmvct.chapter2.web.controller.HelloWorldController"/>
    

    在csdn论坛找到了问题的原因:maven自动生成的web.xml有问题,schema写的不对,修改后可以正确取值:
    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">
      <display-name>Archetype Created Web Application</display-name>
    
      <servlet>
        <servlet-name>mymvct</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>mymvct</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    </web-app>
    
  • 相关阅读:
    IDS与IPS功能分析
    CentOS 命令大全
    仿京东放大镜
    CSS垂直水平居中方法总结
    Java学习之计算机基础(一)
    Java 代码学习之理解数据类型中的坑
    Java 代码学习之数组的初始化
    Java中的比较总结
    Java Random介绍
    手机网站开发必修课[2]:浏览器兼容性测试
  • 原文地址:https://www.cnblogs.com/jpfss/p/7525944.html
Copyright © 2020-2023  润新知