SpringMVC(1)基本使用之获取参数的注意事项

解释全在注解里面,此处不做详细说明

Spring获取参数的几种方式

package controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import pojo.Student;

//@Controller标注是一个类是Web控制器,
//url可以对这个类进行访问
@Controller
public class TestController {
	
	//因为web.xml里面定义了“映射路径.do”
	//所以,URL路径为t.do
	@RequestMapping("t")
	public void test() {
		//测试路径:http://localhost:8080/testMVC7/t.do
		System.out.println("hello!");
	}
	
	/*@RequestMapping("t")//报错,不允许出现2个相同的url
	public void test0(String name) {//注意:url拼接的名字需要和形参的变量名一致,不然会无法获取值
		//测试路径:http://localhost:8080/testMVC7/t.do?name=tom
		System.out.println(name+"hello!");
	}*/
	
	@RequestMapping("t1")
	public void test1(@RequestParam("name")String mingzi) {//如果url拼接的名字需要和形参的变量名不一致,需要用@RequestParam("name")注解解决
		//测试路径:http://localhost:8080/testMVC7/t1.do?name=tom
		System.out.println(mingzi+"hello!");//tomhello!
	}
	
	@RequestMapping("t2")
	public void test2(String name,String gender) {//可以有多个形参
		//测试路径:http://localhost:8080/testMVC7/t2.do?name=tom&gender=男
		System.out.println(name+" "+gender+" "+"hello!");//tom 男 hello!
	}
	
	@RequestMapping("t3")
	public void test3(int age) {//网络中传输的都是字符串,但是,这里int类型自动转型
		//测试路径:http://localhost:8080/testMVC7/t3.do?age=10
		System.out.println(age+"hello!");//10hello!
	}
	
	@RequestMapping("t4")
	public void test4(Student stu) {//可以传一个pojo,只要和对象的变量名一致,就会自动赋值给对象
		//测试路径:http://localhost:8080/testMVC7/t4.do?name=zhangsan&age=10
		System.out.println(stu.getName()+" "+stu.getAge()+" "+stu.getGender());//zhangsan 10 null
	}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <!-- 配置Servlet -->
  <!-- 所有*.do的路径都会通过DispatcherServlet处理-->
  <!-- 将*.do变为*,会拦截所有静态文件-->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!-- 设置配置文件的名字 -->
      <param-name>contextConfigLocation</param-name>
      <!-- classpath是指在tomcat——》webapps——》项目工程名——》WEB-INF——》classes下的文件 -->
      <!-- springmvc.xml可以保存在src下面,编译时,src下会自动编译到classes文件夹下 -->
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!-- 定义映射路径,所有的映射路径都会默认加上.do -->
    <url-pattern>*.do</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <!-- 所有路径 -->
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 定义默认欢迎页面 -->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

SpringMVC.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- 下面的xmlns是定义xml的解释性文件,确定xml的书写规则 -->
<beans default-lazy-init="true"
	xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-3.0.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 使用注解的包,包括子集 -->
	<context:component-scan base-package="controller" />
	<!-- 通过注解,把URL映射到Controller上,该标签默认注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
	<mvc:annotation-driven />
	<!-- 视图解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		
		<!-- 当返回值类型为void时,默认跳转到 “前缀+映射路径+后缀”页面 -->
		<!-- 跳转页面的前缀 -->
		<property name="prefix" value="/WEB-INF/" />
		<!-- 跳转页面的后缀 -->
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- 允许通过的静态资源路径 -->
	<mvc:resources mapping="/js/**" location="/js/" />
	<mvc:resources mapping="/css/**" location="/css/" />
	<mvc:resources mapping="/fonts/**" location="/fonts/" />
</beans>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值