工程結構

在lib下面加入jar包,jar圖片如下所示:

2,配置web.xml,也就是sPRing mvc設置啟動的servlet
[html] view plain copy<servlet> <servlet-name>hello</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param><!-- 這兩行是重新定位spring的配置文件路徑,默認是在WEB-INF下面的hello-servlet.xml --> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/web-main.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> 3,書寫spring的配置文件[html] view plain copy<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.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"> <!-- 開始spring mvc的注解 --> <mvc:annotation-driven/> <!-- 這樣根目錄下面的resource的文件(.CSS,.js等)就不會被spring的DispatchServlet進行過濾 --> <mvc:resources location="/resources/" mapping="/resources/**"/> <!-- 配置注解掃描的包路徑 --> <context:component-scan base-package="cn.edu.hj.controller"></context:component-scan> <!-- 配置action中返回的視圖配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 上傳文件時需要做的配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5000000"></property> </bean> </beans>4,書寫action中的代碼
int userId = ServletRequestUtils.getIntParameter(request, "userId",-1);
ServletRequestUtils是Spring 2.0新增的工具類,可以方便地按類型獲取請求參數的值,它位于org.springframework.web.bind包中。
[java] view plain copypackage cn.edu.hj.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; @Controller //@RequestMapping(value = "/hello")//表示要訪問這個action的時候都要加上這個/hello路徑 public class HelloController { /* 接收參數getParameter()的時候: * 如果地址欄/springmvc/hello.htm上面沒有傳遞參數,那么當id為int型的時候會報錯,當id為Integer的時候值為null * 當地址欄為/springmvc/hello.htm?id=10的時候,action中有三種接收方式 * 1、String hello(@RequestParam(value = "userid") int id),這樣會把地址欄參數名為userid的值賦給參數id,如果用地址欄上的參數名為id,則接收不到 * 2、String hello(@RequestParam int id),這種情況下默認會把id作為參數名來進行接收賦值 * 3、String hello(int id),這種情況下也會默認把id作為參數名來進行接收賦值 * 注:如果參數前面加上@RequestParam注解,如果地址欄上面沒有加上該注解的參數,例如:id,那么會報404錯誤,找不到該路徑 */ @RequestMapping(value = "/hello.htm") public String hello(int id){//getParameter()的方式 System.out.println("hello action:"+id); // return "hello"; return "redirect:/index.jsp";//不能重定向web-info里面的文件,而且需要寫上絕對路徑 } //返回頁面參數的第一種方式,在形參中放入一個map @RequestMapping(value = "/hello1.htm") public String hello(int id,Map<String,Object> map){ System.out.println("hello1 action:"+id); map.put("name", "huangjie"); return "hello"; } //返回頁面參數的第二種方式,在形參中放入一個Model @RequestMapping(value = "/hello2.htm") public String hello2(int id,Model model){ System.out.println("hello2 action:"+id); model.addAttribute("name", "huangjie"); //這個只有值沒有鍵的情況下,使用Object的類型作為key,String-->string model.addAttribute("ok"); return "hello"; } //得到request,response,session等,只要在方法形參中聲明參數即可 @RequestMapping(value = "/hello3.htm") public String hello3(HttpServletRequest request){ String id = request.getParameter("id"); System.out.println("hello3 action:"+id); return "hello"; } } 5,書寫返回視圖的jsp頁面/WebRoot下面的index.jsp
[html] view plain copy<body> This is my JSP page. <br> </body> /WebRoot/WEB-INF下面的hello.jsp[html] view plain copy<body> This is my hello page. <br> name:${name } <br> value:${string} <br> </body>新聞熱點
疑難解答