spring-mvc 入门

这是学习官网的spring mvc 例子:

http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/

我这里是用的spring 3.2.2 ,地址:

http://www.springsource.org/download/community

关于变化,以及历史信息:解压文件看里面的.txt 文档,以及docs 目录下的changelog 文档

 其他要加入:commons-logging.jar 。如果使用其他标签,还需要其他相关jar

 

入门实例,这是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">
  <display-name>springmvc</display-name>
  	<servlet>
  		<servlet-name>spring</servlet-name>
  		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  		  <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
  	</servlet>
  	<servlet-mapping>
  		<servlet-name>spring</servlet-name>
  		<url-pattern>/</url-pattern>
  	</servlet-mapping>
</web-app>
 

 

关于DispatcherServlet 作用和介绍,可以看上面链接地址,或者参考:

http://jinnianshilongnian.iteye.com/blog/1602617

 

 

@Controller
public class HomeController {
	@RequestMapping("/")
	public String home(){
		System.out.println("Is here!");
		return "home";
	}
}
 

 

这里用了"/" 表示,是默认请求路径,也就是根路径,比如:http://localhost:8080/springmvc/

实际上 @Controller 下面,默认有@RequestMapping("/"),由Controller 注解控制转发。

当然也可以指定请求,那么下面方法的目录就会承接起来。 比如:

 

 

@Controller
@RequestMapping("/app")
HomeController{
   @RequestMapping("/")
	public String home(){
		System.out.println("Is here!");
		return "home";
	}
}

 

这时候请求就变成了:http://localhost:8080/springmvc/app

 

有关@RequestMapping 和  @Controller 的 参考:

http://jinnianshilongnian.iteye.com/blog/1608234

http://jinnianshilongnian.iteye.com/blog/1684403

return "home"; 这里默认是根目录下的home 文件。当然我这里默认设置成了JSP。有两种配置方法。

@Configuration
public class AppConfig {
	@Bean
	public ViewResolver  viewResolver(){
                // 这是代码配置
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/");
		resolver.setSuffix(".jsp");
		return resolver;
	}
}

 

springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
 	<!-- 可以扫描,annotation定义的类 ,使用了context:component-scan,可以取消,效果一样-->
 	<context:annotation-config />
   
    <!-- Enables the Spring MVC @Controller programming model <mvc:default-servlet-handler/> -->
    <!-- 这里是对请求路径,以及返回的一些控制,具体要看源码 -->
    <mvc:annotation-driven />
    
     <!-- Scans within the base package of the application for @Components to configure as beans -->
    <!-- 这里是你扫描的位置,包括@Controller, @Service, @Configuration -->
    <context:component-scan base-package="com.home" />
    
    <!-- 这是XML 配置,另一种形式 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

 

上面的类,都在com.home 下,不然扫描不到。

最后建立一个home.jsp 页面就行了

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	Hello World!..
</body>
</html>

 

http://localhost:8080/springmvc/ 就能访问。

具体的工作流程,可参考:http://jinnianshilongnian.iteye.com/blog/1602617

 

猜你喜欢

转载自greemranqq.iteye.com/blog/1858336