springmvc配置ssl_spring MVC配置详解
現在主流的Web MVC框架除了Struts這個主力 外,其次就是Spring MVC了,因此這也是作為一名程序員需要掌握的主流框架,框架選擇多了,應對多變的需求和業務時,可實行的方案自然就多了。不過要想靈活運用Spring MVC來應對大多數的Web開發,就必須要掌握它的配置及原理。
一、Spring MVC環境搭建:(Spring 2.5.6 + Hibernate 3.2.0)
1. jar包引入
Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar
Hibernate 3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相應數據庫的驅動jar包
SpringMVC是一個基于DispatcherServlet的MVC框架,每一個請求最先訪問的都是DispatcherServlet,DispatcherServlet負責轉發每一個Request請求給相應的Handler,Handler處理以后再返回相應的視圖(View)和模型(Model),返回的視圖和模型都可以不指定,即可以只返回Model或只返回View或都不返回。
DispatcherServlet是繼承自HttpServlet的,既然SpringMVC是基于DispatcherServlet的,那么我們先來配置一下DispatcherServlet,好讓它能夠管理我們希望它管理的內容。HttpServlet是在web.xml文件中聲明的。
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
*.do
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:config/applicationContext.xml
spring-servlet.xml配置
spring-servlet這個名字是因為上面web.xml中標簽配的值為spring(spring),再加上“-servlet”后綴而形成的spring-servlet.xml文件名,如果改為springMVC,對應的文件名則為springMVC-servlet.xml。
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
DispatcherServlet會利用一些特殊的bean來處理Request請求和生成相應的視圖返回。
關于視圖的返回,Controller只負責傳回來一個值,然后到底返回的是什么視圖,是由視圖解析器控制的,在jsp中常用的視圖解析器是InternalResourceViewResovler,它會要求一個前綴和一個后綴
在上述視圖解析器中,如果Controller返回的是blog/index,那么通過視圖解析器解析之后的視圖就是/jsp/blog/index.jsp。
主要是說說Controller.
一個類使用了@Controller進行標記的都是Controller
packagecontroller;importjavax.servlet.http.HttpServletRequest;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importentity.User;
@Controller//類似Struts的Action
public classTestController {
@RequestMapping("test/login.do") //請求url地址映射,類似Struts的action-mapping
public String testLogin(@RequestParam(value="username")String username, String password, HttpServletRequest request) {//@RequestParam是指請求url地址映射中必須含有的參數(除非屬性required=false)//@RequestParam可簡寫為:@RequestParam("username")
if (!"admin".equals(username) || !"admin".equals(password)) {return "loginError"; //跳轉頁面路徑(默認為轉發),該路徑不需要包含spring-servlet配置文件中配置的前綴和后綴
}return "loginSuccess";
}
@RequestMapping("/test/login2.do")public ModelAndView testLogin2(String username, String password, intage){//request和response不必非要出現在方法中,如果用不上的話可以去掉//參數的名稱是與頁面控件的name相匹配,參數類型會自動被轉換
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {return new ModelAndView("loginError"); //手動實例化ModelAndView完成跳轉頁面(轉發),效果等同于上面的方法返回字符串
}return new ModelAndView(new RedirectView("../index.jsp")); //采用重定向方式跳轉頁面//重定向還有一種簡單寫法//return new ModelAndView("redirect:../index.jsp");
}
@RequestMapping("/test/login3.do")publicModelAndView testLogin3(User user) {//同樣支持參數為表單對象,類似于Struts的ActionForm,User不需要任何配置,直接寫即可
String username =user.getUsername();
String password=user.getPassword();int age =user.getAge();if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {return new ModelAndView("loginError");
}return new ModelAndView("loginSuccess");
}
@Resource(name= "loginService") //獲取applicationContext.xml中bean的id為loginService的,并注入
private LoginService loginService; //等價于spring傳統注入方式寫get和set方法,這樣的好處是簡潔工整,省去了不必要得代碼
@RequestMapping("/test/login4.do")publicString testLogin4(User user) {if (loginService.login(user) == false) {return "loginError";
}return "loginSuccess";
}
}
以上4個方法示例,是一個Controller里含有不同的請求url,也可以采用一個url訪問,通過url參數來區分訪問不同的方法,代碼如下:
packagecontroller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test2/login.do") //指定唯一一個*.do請求關聯到該Controller
public classTestController2 {
@RequestMappingpublic String testLogin(String username, String password, intage) {//如果不加任何參數,則在請求/test2/login.do時,便默認執行該方法
if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {return "loginError";
}return "loginSuccess";
}
@RequestMapping(params= "method=1", method=RequestMethod.POST)publicString testLogin2(String username, String password) {//依據params的參數method的值來區分不同的調用方法//可以指定頁面請求方式的類型,默認為get請求
if (!"admin".equals(username) || !"admin".equals(password)) {return "loginError";
}return "loginSuccess";
}
@RequestMapping(params= "method=2")public String testLogin3(String username, String password, intage) {if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {return "loginError";
}return "loginSuccess";
}
}
其實RequestMapping在Class上,可看做是父Request請求url,而RequestMapping在方法上的可看做是子Request請求url,父子請求url最終會拼起來與頁面請求url進行匹配,因此RequestMapping也可以這么寫:
packagecontroller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test3/*") //父request請求url
public classTestController3 {
@RequestMapping("login.do") //子request請求url,拼接后等價于/test3/login.do
public String testLogin(String username, String password, intage) {if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {return "loginError";
}return "loginSuccess";
}
}
在SpringMVC中常用的注解還有@PathVariable,@RequestParam,@PathVariable標記在方法的參數上,利用它標記的參數可以利用請求路徑傳值,看下面一個例子
@RequestMapping(value="/comment/{blogId}", method=RequestMethod.POST)public void comment(Comment comment,@PathVariable int blogId, HttpSession session, HttpServletResponse response) throwsIOException {
}
在該例子中,blogId是被@PathVariable標記為請求路徑變量的,如果請求的是/blog/comment/1.do的時候就表示blogId的值為1. 同樣@RequestParam也是用來給參數傳值的,但是它是從頭request的參數里面取值,相當于request.getParameter("參數名")方法。
在Controller的方法中,如果需要WEB元素HttpServletRequest,HttpServletResponse和HttpSession,只需要在給方法一個對應的參數,那么在訪問的時候SpringMVC就會自動給其傳值,但是需要注意的是在傳入Session的時候如果是第一次訪問系統的時候就調用session會報錯,因為這個時候session還沒有生成。
總結
以上是生活随笔為你收集整理的springmvc配置ssl_spring MVC配置详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 昌州是哪个省
- 下一篇: 好听的公司名称注册大全533个