一、环境搭建
1、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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>pro name</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pro name</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 统一编码的配置语句 start-->
<filter>
<filter-name>CharacterFilter</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>CharacterFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- end -->
</web-app>
2、servlet-XML配置文件
<beans **xmlns:mvc="http://www.springframework.org/schema/mvc"**
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: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.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
**http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"**
>
</beans>
3、InternalResourceViewResolver方式解析时,配置文件的书写方式
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix代表前缀 suffix代表后缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
controller类示例
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
System.out.println("Welcome");
return new ModelAndView("welcome");
}
4、使用默认注解方式解析,配置文件的书写方式
<!-- 注解扫描器 -->
<context:component-scan base-package="test.dayone.controller"/>
<!-- 上方加粗部分为此标签需引用的地址 -->
<mvc:annotation-driven/>
controller类示例
@RequestMapping("/welcome")
public String welcome(){
System.out.println("welcome");
return "welcome";
}
二、view → controller传值
1、使用@RequestParam方式传值
@RequestMapping({"/hello","/"})
public String hello(@RequestParam("username") String username){
System.out.println("hello");
System.out.println(username);
return "hello";
}
注意:使用此方法传值时,参数默认是地址的一部分,此时若不传参,则会报404错误(可以用于传必不可少的参数)
2、直接传值,即使用函数参数传值
@RequestMapping({"/hello","/"})
public String hello(String username){
System.out.println("hello");
System.out.println(username);
return "hello";
}
三、controller → view传值
1、使用Map 传值
@RequestMapping({"/hello","/"})
public String hello(String username, Map(String,Object) context){
System.out.println("hello");
context.put("username",username);
System.out.println(username);
return "hello";
}
jsp页面:
hello ${username} !!!
2、使用Model传值
I、键值对
@RequestMapping({"/hello","/"})
public String hello(String username,Model model){
System.out.println("hello");
model.addAttribute("username",username);
System.out.println(username);
return "hello";
}
jsp页面:
hello ${username} !!!
II、只传值的情况
@RequestMapping({"/hello","/"})
public String hello(String username,Model model){
System.out.println("hello");
//此时的key,默认是使用对象的类型作为key
model.addAttribute(username);
System.out.println(username);
return "hello";
}