(7)spring表达式
注:读取bean的属性
1. 使用注解来简化配置文件
(1)什么是组件扫描?
容器会扫描指定的包及其子包下面的所有的类,如果该类前面有特定的注解比如@Component),则容器会将其纳入容器进行管理(相当于在配置文件里面有一个bean元素)。
(2)如何进行组件扫描?
step1. 在类前面添加特定的注解,比如 @Component注:默认的id是首字母小写之后的类名。
step2. 在配置文件当中,配置组件扫描。
(3)依赖注入相关的注解
1)@Autowired @Qualifier注:该注解支持set方法和构造器注入。
2)@Resource注:该注解只支持set方法注入。
(4)注入基本类型的值和spring表达式的值
SpringMVC
(1)什么是SpringMVC?
用来简化基于MVC架构的web应用程序开发的框架。注:SpringMVC是spring中的一个模块。
(2)五大组件
1)有哪五大组件?DispatcherServlet 前端控制器HandlerMapping 映射处理器Controller 处理器ModelAndViewViewResolver 视图解析器2)它们之间的关系a.请求发送给DispatcherServlet来处理,DispatcherServlet会依据HandlerMapping的配置调用对应的Controller来处理。b.Controller将处理结果封装成ModelAndView,然后返回给DispatcherServlet。c.DispatcherServlet会依据ViewResolver的解析调用对应的视图对象(比如jsp)来生成相应的页面。注:视图部分可以使用jsp,也可以使用其它的视图技术,比如freemarker,velocity等等。
(3)编程步骤
step1. 导包spring-webmvcstep2. 添加配置文件。step3. 配置DispatcherServlet。step4. 写Controller。step5. 写jsp。step6.在配置文件当中,添加HandlerMapping,ViewResolver的配置。
代码演示:
/springmvc-day01/src/main/java/controller/HelloController.java
package controller;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;/*** 处理器**/
public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {System.out.println("handleRequest()");return new ModelAndView("hello");}}
/springmvc-day01/src/main/resources/spring-mvc.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:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"><bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="/hello.do">helloController</prop></props></property></bean><bean id="helloController" class="controller.HelloController" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/" /><property name="suffix" value=".jsp" /></bean></beans>
/springmvc-day01/src/main/webapp/WEB-INF/hello.jsp
<h1>Hello Kitty</h1>
/springmvc-day01/src/main/webapp/WEB-INF/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_2_5.xsd" version="2.5"><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern>
</servlet-mapping></web-app>
/springmvc-day01/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>welkin</groupId><artifactId>springmvc-day01</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.2.8.RELEASE</version></dependency></dependencies>
</project>