SpringMVC3.0+rest小例子 博客分类: J2EE_SpringMVC springmvcrest
程序员文章站
2024-03-16 19:42:40
...
先看web.xml配置
<!-- 像js,css,gif等静态文件,需要配置为默认的servlet -->
spring-servlet.xml文件配置,注:XXXX-servlet.xml的XXXX就是上边<servlet-name>spring</servlet-name>中
的名称,如果上边为<servlet-name>mvc</servlet-name>则这个文件名为mvc-servelt.xml。
controller类
两个jsp页面
\WEB-INF\view\welcome.jsp
\WEB-INF\view\login\hello.jsp
运行
<!-- 像js,css,gif等静态文件,需要配置为默认的servlet -->
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> <!-- url配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的 访问 --> </servlet-mapping>
spring-servlet.xml文件配置,注:XXXX-servlet.xml的XXXX就是上边<servlet-name>spring</servlet-name>中
的名称,如果上边为<servlet-name>mvc</servlet-name>则这个文件名为mvc-servelt.xml。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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"> <!-- 自动扫描bean,把作了注解的类转换为bean --> <context:component-scan base-package="com.mvc.rest" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /> </beans>
controller类
package com.mvc.rest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class RestController { public RestController(){ } @RequestMapping(value = "/login/{user}", method = RequestMethod.GET) public ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response, @PathVariable("user") String user, ModelMap modelMap) throws Exception { modelMap.put("loginUser", user); return new ModelAndView("/login/hello", modelMap); } @RequestMapping(value = "/welcome", method = RequestMethod.GET) public String registPost() { return "/welcome"; } }
两个jsp页面
\WEB-INF\view\welcome.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> 欢迎 </body> </html>
\WEB-INF\view\login\hello.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> 你好:<%=request.getAttribute("loginUser") %> </body> </html>
运行