springMVC可以实现rest风格的链接使用。非常方便
1.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>01_spring_mvc</display-name> <!-- springMVC 1.创建servlet --> <servlet> <!-- 需要在WEB-INF下面创建对应的hello-servlet.xml --> <servlet-name>hello</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <!-- 捕获所有请求,基于rest --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
2.hello-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- springMVC 2.创建对应的servlet --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 开启SpringMVC --> <mvc:annotation-driven/> <context:component-scan base-package="cn.edu.zttc.controller"></context:component-scan> <!-- 配置视图的前缀和后缀 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
3.hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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>hello</title> </head> <body> hello <br> ${hello} <br> ${string} </body> </html>
4.HelloController.java
package cn.edu.zttc.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; /** * 基于@的MVC处理 * @author admin * */ @Controller public class HelloController { /** * SpringMVC * 设定访问规则:@RequestMapping(value={"/","/hello"}) * 传递参数三种方法: * 1.@RequestParam("userId") int id * 2.@RequestParam() int id * 3.int id * 如果设置了@RequestMapping;则访问的时候必须加入?和参数,否则不会访问当前方法 * * Struts2的 * * @param id * @return */ @RequestMapping(value={"/","/hello"}) public String hello(@RequestParam("userId") int id,Map<String, Object> map){ System.out.println("hello"); System.out.println(id); map.put("hello", "hello world"); return "hello"; } /** * 使用Model * @param model * @return */ // public String say(@RequestParam()int id,Model model){ @RequestMapping(value="/say") public String say(Model model){ model.addAttribute("hello", "say hello world"); //使用Object的类型作为key,string-->String //访问:${string} model.addAttribute("ok"); return "hello"; } /** * 获取前台传递的request参数 * @param req * @return */ @RequestMapping("/req") public String req(HttpServletRequest req){ System.out.println(req.getParameter("username")); return "hello"; } }
下面附上项目结构图:
以及项目所需要的包:
作者:devilzy2656 发表于2013-2-26 9:50:53 原文链接
阅读:0 评论:0 查看评论