• springMvc 通过url传值,实现访问


    springMvc 通过url传值,实现访问

     

    1、创建web项目,引入相关jar包,并完成相应配置,在上一篇博客已经实现

    2、在WEB-INF下创建jsp文件夹,并创建hello.jsp文件

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Hello</title>
     8 </head>
     9 <body>
    10 你好,${name }! 
    11 
    12 </body>
    13 </html>

    3、在与web.xml同目录下创建mvcdemo-servlet.xml文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:mvc="http://www.springframework.org/schema/mvc"
     5     xmlns:context="http://www.springframework.org/schema/context"
     6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
     7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
     9     <!-- 包扫描 -->
    10     <context:component-scan    base-package="com.GetcharZp.mvcdemo" />
    11         
    12     <!-- mvc注解驱动 -->
    13     <mvc:annotation-driven />
    14     
    15     <!-- 视图解析器 ,支持多种视图 -->
    16     <bean
    17         class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    18         <property name="viewResolvers">
    19             <list>
    20                 <bean
    21                     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    22                     <property name="viewClass"
    23                         value="org.springframework.web.servlet.view.JstlView" />
    24                     <property name="prefix" value="/WEB-INF/jsp/" />
    25                     <property name="suffix" value=".jsp" />
    26                 </bean>
    27                 <bean
    28                     class="org.springframework.web.servlet.view.BeanNameViewResolver" />
    29             </list>
    30         </property>
    31         <property name="defaultViews">
    32             <list>
    33                 <bean
    34                     class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>
    35             </list>
    36         </property>
    37     </bean>
    38 
    39 </beans>

    4、创建com.GetcharZp.mvcdemo包,并在其下创建HelloController.java代码

     1 package com.GetcharZp.mvcdemo;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.ui.ModelMap;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 
     7 @Controller
     8 public class HelloController {
     9 
    10     @RequestMapping(value="h")
    11     public String hellog(String name, ModelMap mm){
    12         mm.addAttribute("name", name);
    13         return "hello";
    14     }
    15 }

    5、web.xml进行如下配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     5     version="3.1">
     6     <display-name>mvcdemo</display-name>
     7 
     8     <!-- 启动监听 -->
     9     <listener>
    10         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    11     </listener>
    12     
    13     <!-- 分发的Servlet -->
    14     <servlet>
    15         <servlet-name>mvcdemo</servlet-name>
    16         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    17     </servlet>
    18     <servlet-mapping>
    19         <servlet-name>mvcdemo</servlet-name>
    20         <url-pattern>/</url-pattern>
    21     </servlet-mapping>
    22     <!-- 强制将根容器的配置指向mvc的容器 -->
    23     <context-param>
    24         <param-name>contextConfigLocation</param-name>
    25         <param-value>/WEB-INF/mvcdemo-servlet.xml</param-value>
    26     </context-param>
    27     <!-- 字符编码集过滤器  -->
    28     <filter>
    29         <filter-name>characterEncodingFilter</filter-name>
    30         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    31         <init-param>
    32             <param-name>encoding</param-name>
    33             <param-value>UTF-8</param-value>
    34         </init-param>
    35         <init-param>
    36             <param-name>forceEncoding</param-name>
    37             <param-value>true</param-value>
    38         </init-param>
    39     </filter>
    40     <filter-mapping>
    41         <filter-name>characterEncodingFilter</filter-name>
    42         <url-pattern>/*</url-pattern>
    43     </filter-mapping>
    44 
    45 </web-app>
  • 相关阅读:
    Shell while循环
    Shell for循环
    针对各主流数据mysql、sqlserver、oracle中文乱码问题。
    robots.txt网站爬虫文件设置
    MySql数据类型
    重温国产thinkphp
    CI分支kohana在线文档
    JIRA官方:为什么要用JIRA?
    百度地图V2.0实践项目开发工具类bmap.util.js V1.4
    Eclipse被汉化后恢复EN模式
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9273829.html
Copyright © 2020-2023  润新知