Spring4-快速入门

来源:互联网 发布:useragent windows nt 编辑:程序博客网 时间:2024/04/30 01:22

Spring4-快速入门之搭建简单的web项目

1.配置web.xml文件,在web服务器启动时自动启动spring容器,配置Spring MVC框架

<?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">    <!-- 加载指定路径下的spring配置文件 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:smart-context.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 设置spring mvc的主控servlet -->    <servlet>        <servlet-name>smart</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>2</load-on-startup>    </servlet>    <!-- spring mvc处理的url -->    <servlet-mapping>        <servlet-name>smart</servlet-name>        <url-pattern>*.do</url-pattern>    </servlet-mapping></web-app>

smart-context.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:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <context:component-scan base-package="com.test.spring4"></context:component-scan></beans>  

使用Spring MVC也需要配置spring配置文件,默认文件名为servlet名-servlet.xml,例如此处为:smart-servlet.xml,将这个配置文件放到/WEB-INF目录下,具体内容如下:

<?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:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <!-- 指定controller类的路径 -->  <context:component-scan base-package="com.test.spring4.web"></context:component-scan>  <!-- 配置视图解析器,讲ModelAndView及字符串解析为具体的页面 -->  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>    <property name="prefix" value="/WEB-INF/jsp/"></property><!-- 指定视图名前添加的前缀 -->    <property name="suffix" value=".jsp"></property><!-- 指定视图名后添加的后缀 -->    <!-- 例如视图名为success,则会访问/WEB-INF/jsp/success.jsp页面 -->  </bean>

2.编写Controller

@Controllerpublic class TestController {    @RequestMapping(value="/test.do")    public ModelAndView test(String username,String password) {        System.out.println("username:"+username);        System.out.println("password:"+password);        ModelAndView mv = new ModelAndView();        mv.addObject("username", username);        mv.setViewName("success");        return mv;    }}

这里将TestController类通过@Controller注解声明为一个Controller,@RequestMapping设置访问的url,根据返回的视图,会跳转到/WEB-INF/jsp/success.jsp页面
3.启动web服务,访问http://locahost:8080/spring4/test.do?username=user&password=123456,执行效果如下。
这里写图片描述