spring 3 MVC 笔记 1
2011-08-24 15:44
134 查看
Spring3 MVC 学习笔记:用 STS 创建 spring template project, 选择spring MVC template,创建成功后,一个最基本的 hello world 应用就创建成功了。
配置:jar包:
web.xml
${servlet-name}-servlet.xml
Controller Java Class :
配置:jar包:
web.xml
<?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"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--如果不配置指定spring mvc config文件的位置,spring 会默认加载 WEB-INF/${servlet-name}-servlet.xml--> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
${servlet-name}-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 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 --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="home.dong.springmvc" /> </beans:beans>
Controller Java Class :
package home.dong.springmvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * @Controller 标明这是一个controller. * @RequestMapping(value="/simple") 定义类级别的request mapping 的访问路径. * @author tiger * */ @Controller @RequestMapping(value="/simple") public class SimpleMapping { /** * 将跳转到 WEB-INF/view/simple.jsp 返回的字符串就是jsp的文件名。 * 此访问路径为 http://localhost:8080/application_name/simple/simpleGet * @return jsp 文件名 */ @RequestMapping(value="/simpleGet") public String simpleGetMethod(){ return "simple"; } /** * 方法前加了 @ResponseBody 表明次方法将返回字符串 * 返回字符串<h5>this is simple world</h5> * 此访问路径为 http://localhost:8080/application_name/simple/otherSimpleGet * @return String 字符串 */ @RequestMapping(value="/otherSimpleGet",method=RequestMethod.GET) public @ResponseBody String otherSimpleGetMethod(){ return "<h5>this is simple world</h5>"; } }
相关文章推荐
- MVC使用Spring.Net应用IOC(依赖倒置)学习笔记3
- Java Web 学习笔记(四) 基于 SpringMVC+BootStrap 创建WebApp
- 【Spring学习笔记-MVC-12】Spring MVC视图解析器之ResourceBundleViewResolver
- 【学习笔记】SpringMvc+WebUploader整合处理
- Spring+SpringMVC+Mybatis框架整合例子(SSM) 学习笔记
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-001- 配置SpringFlow(flow-executor、flow-registry、FlowHandlerMapping、FlowHandlerAdapter)
- Spring+SpringMVC+Hibernate整合笔记(一)项目结构与配置
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-003-Pizza例子的基本流程
- 基于annotation注解的Spring3 MVC之一 HelloWorld 例子学习笔记
- springmvc+hibernate+security整合笔记
- spring 3 MVC 笔记 2
- spring 3 MVC 笔记 4
- Log4j2 配置笔记(Eclipse+maven+SpringMVC)
- SPRING IN ACTION 第4版笔记-第八章Advanced Spring MVC-005-Pizza例子的订单流程()
- SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法
- Spring Portlet MVC 学习笔记
- spring-mvc 笔记——RequestParam注解
- SSH(Spring+Struts+Hibernate)学习笔记(二)Struts概览与MVC简述
- SpringMVC学习笔记(二)使用IntelliJ IDEA开发Spring MVC HelloWorld(基于Maven)