Spring MVC框架配置
阿新 • • 發佈:2018-12-18
第一步:匯入包 aopalliance-1.0.jar aspectjweaver-1.8.13.jar commons-logging-1.2.jar javax.servlet-api-3.1.0.jar jstl-1.2.jar spring-aop-3.2.13.RELEASE.jar spring-beans-3.2.13.RELEASE.jar spring-context-3.2.13.RELEASE.jar spring-core-3.2.13.RELEASE.jar spring-expression-3.2.13.RELEASE.jar spring-web-3.2.13.RELEASE.jar spring-webmvc-3.2.13.RELEASE.jar
第二步:配置檔案springmvc-servlet.xml配置
<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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置處理器對映 HandlerMapping.使用了註解就可以不用這個了,因為不用註解這裡需要配置多個,controller類也不用繼承AbstractController介面了--> <!--<bean name="/index.html" class="cn.kgc.controller.IndexController"/>--> <!--配置檢視解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--字首--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--字尾--> <property name="suffix" value=".jsp"/> </bean> <!-- 配置掃描註解的包 --> <context:component-scan base-package="cn.kgc.controller" /> <!-- 註冊HandlerMapper、HandlerAdapter兩個對映類 --> <mvc:annotation-driven /> <!-- 訪問靜態資源,mapping:將靜態資源對映到指定的路徑下;location:本地靜態資原始檔所在的目錄 --> <mvc:default-servlet-handler /> <mvc:resources mapping="WEB-INF/statics/**" location="WEB-INF/statics/"/> </beans>
第三步:配置web.xml檔案
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <!--2.4版本以上可以不用加er語句,也能認到er表示式--> <web-app> <!--給上下文傳引數(這個會在Action裡面用到),web服務啟動時載入的xml檔案--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:mybatis-spring.xml </param-value> </context-param> <!--加入一個監聽器,在服務一啟動的時候把Spring的配置檔案的mybatis-spring.xml讀取,以後不用每執行一次方法都讀取xml檔案--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--springMvc核心控制器的配置DispatchServlet--> <servlet> <servlet-name>springmvc</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> <!-- / 攔截所有的URL請求--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第四步:建立Controller(處理請求的控制器,也就是servlet) 建立Controller層 新建控制類
@Controller
@RequestMapping("/user") // "/user" 訪問URL
public class IndexController extends BasDao {
private Logger logger=Logger.getLogger(IndexController.class);
// "/user/hello" 訪問URL
@RequestMapping("/hello")
public ModelAndView index(HttpServletRequest request){
ApplicationContext context=getApplicatIonContext(request);
DetailService dsi=(DetailService)context.getBean("detailServiceImpl");
int i=dsi.count();
logger.info("呼叫:index 方法,引數:null,執行結果:"+i);
return new ModelAndView("hello"); //返回邏輯檢視名,hello.jsp頁面
}
@RequestMapping(value = "/hello1",method = RequestMethod.GET,params = "username") //value:請求URL,method:請求方式,params:請求引數
public String welcome(@RequestParam(required = false,value = "username") String username,HttpServletRequest request){ //value:引數別名,可以省略,required:是否必須
ApplicationContext context=getApplicatIonContext(request);
DetailService dsi=(DetailService)context.getBean("detailServiceImpl");
int i=dsi.count();
logger.info("呼叫:index 方法,引數:"+username+",執行結果:"+i);
return "hello";
}
@RequestMapping("/index1")
public ModelAndView index(@RequestParam(defaultValue = "10",required = false) Integer age, String username,HttpServletRequest request){ //不給@RequestParam註解,url引數可給可不給,不給賦null值。但只能是引用型別或者包裝型別
//defaultValue=“10”:如果不給url引數賦值,就給賦指定值10,
ApplicationContext context=new ClassPathXmlApplicationContext("mybatis-spring.xml");
DetailService dsi=(DetailService)context.getBean("detailServiceImpl");
int i=dsi.count();
ModelAndView mav=new ModelAndView();
mav.addObject("username",username+age); //把引數傳送到前段
mav.setViewName("hello"); //用那一個頁面顯示
logger.info("呼叫:index 方法,引數:"+username+",執行結果:"+i);
return mav;
}
@RequestMapping("/index2")
public String index2(String username, Model model){
ApplicationContext context=new ClassPathXmlApplicationContext("mybatis-spring.xml");
DetailService dsi=(DetailService)context.getBean("detailServiceImpl");
int i=dsi.count();
model.addAttribute("username",username); //給 model新增資料,直接在前斷顯示
logger.info("呼叫:index 方法,引數:"+username+",執行結果:"+i);
return "hello";
}
}
第五步:建立jsp檢視 第六步:部署伺服器執行