Ⅰ 如何用springMVC 返回一個指定的HTML頁面
用springMVC 返回一個指定的HTML頁面的方法:
1、在controller層需要做返回指定:
@RequestMapping(value = "/jsptest.html", method = RequestMethod.GET)
public String jspTest(Model model) {
model.addAttribute("account", accountService.getAccount());
return "indextest";
}
2、在資源層放html文件
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".html" />
</bean>
3、對靜態資源文件的訪問
<mvc:resources location="/img/" mapping="/img/**" />
<mvc:resources location="/js/" mapping="/js/**" />
Ⅱ spring-restdocs-asciidocto 生產不了html
route('/callback_result', methods = ["POST","GET"])
def callback_result():
try:nm = nmap.PortScanner()
instantiate nmap.PortScanner object
except nmap.PortScannerError:
sys.exit(0)
except:
sys.exit(0)
Ⅲ JSP頁面 里a超鏈接如何帶參數可以在後台controller方法參數里直接接收 用的sp
可以在後台controller方法參數里直接接收,有如下兩者方法:
在controller里不用註解可以這么寫:
public ModelAndView handleRequest(HttpServletRequest req)throws Exception { ModelAndView mv = new ModelAndView();mv;) String key1.addObject("key1", key1);mv.getParameter("key1")mv;, key2);key2" }
用註解可以這么寫:
public ModelAndView handleRequest(@RequestParam(".addObject(".addObject("jsp頁面的請求地址後帶上參數href="xxxx; mv.addObject("key1";req;); mv.setViewName("login/login.jsp",@RequestParam String key2) throws Exception { ModelAndView mv = new ModelAndView();key1"key2"
//, req;, req.getParameter("key1")); return mv; mv.setViewName("login/login.jsp"); return mv.getParameter("key2"))?key1=1&key2=2"
jsp(JavaServer Pages)是由Sun Microsystems公司倡導、許多公司參與一起建立的一種動態網頁技術標准,其網址為http://www.javasoft.com/prodUCts/jsp。該技術為創建顯示動態生成內容的Web頁面提供了一個簡捷而快速的方法。
JSP技術的設計目的是使得構造基於Web的應用程序更加容易和快捷,而這些應用程序能夠與各種Web伺服器,應用伺服器,瀏覽器和開發工具共同工作。 JSP規范是Web伺服器、應用伺服器、交易系統、以及開發工具供應商間廣泛合作的結果。
在傳統的網頁Html文件(*htm,*.html)中加入Java程序片段(Scriptlet)和JSP標記(tag),就構成了JSP網頁(*.jsp)。Web伺服器在遇到訪問JSP網頁的請求時,首先執行其中的程序片段,然後將執行結果以HTML格式返回給客戶。
程序片段可以操作資料庫、重新定向網頁以及發送 email 等等,這就是建立動態網站所需要的功能。所有程序操作都在伺服器端執行,網路上傳送給客戶端的僅是得到的結果,對客戶瀏覽器的要求最低,可以實現無Plugin,無ActiveX,無Java Applet,甚至無Frame。
Ⅳ springmvc 返回一個html字元串是什麼意思
表示的是該路徑下的視圖頁面,在你的視圖解析器里會對該字元串進行解析。
SpringMVC的幾種內返回方式
Ⅳ Spring MVC @requestmapping return 返回一個字元串路徑是什麼意思
表示的是該路徑下的視圖頁面,在你的視圖解析器里會對該字元串進行專解析,然後進行屬渲染,你可以在配置文件里看一下,ViewResolver的suffix是如何配置的,如果是.jsp,那麼比如第一個返回的就是travelShopList.jsp頁面。或者.html那麼就返回的是.html文件,前面的表示該jsp的相對路徑
Ⅵ Spring返回modelandview,如何在html中用jquery接收
這個用jquery是不能直接取到的;用的springmvc的話可以用velocity 或者freemaker 如果執意用jquery的話則可以用jquery ui 可以去查下 用下!
Ⅶ springmvc 在頁面跳轉之後 引入文件的路徑前面加上了 controller 的映射名
創建一個放置來引入文件的源文件夾static 在springmvc的配置文件中設置靜態資源訪問
<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
該文件下的資源就不會被攔截!!!!
Ⅷ spring mvc中怎麼做靜態html
這個不叫靜態,叫偽靜態,是restful風格。比如,在地址欄上顯示的URL是以html結尾的,那在訪問的方法上面@RequestMapping(value="staticpage.html"),訪問時,地址欄自然會是xxx/staticpage.html,這叫偽靜態,如果真的做到靜態的頁面,那還得生成一個靜態的html文件才行。這個怎麼做,參考文檔吧,下面圖是web.xml的一個配置代碼段供參考
上傳不了圖片
<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:application-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Ⅸ spring mvc 配置後,web中的html頁面報404,該怎麼處理
spring mvc 配置後,web中的html頁面報404,路徑錯誤,找不到伺服器資源!檢查下頁面的載入路徑看看。
Ⅹ SPRING MVC架構 URL都被默認加了一級如:頁面寫的是a/a,變成了loginaction/a/a,導致找不到ACTION
1.URL路徑映射
1.1.對一個action配置多個URL映射:
我們把上一篇中的HelloWorldController的index() action方法的@RequestMapping更改為@RequestMapping(value={"/index", "/hello"}, method = {RequestMethod.GET}),這表示對該action配置了/index和/hello兩個映射。運行測試,如下:
可以看到/helloworld/hello請求也成功匹配。
1.2.URL請求參數映射:
這在查詢的時候經常用到,比如我們根據id或編號來獲取某一條記錄。
在HelloWorldController添加一個getDetail的action,代碼如下:
@RequestMapping(value="/detail/{id}", method = {RequestMethod.GET})
public ModelAndView getDetail(@PathVariable(value="id") Integer id){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("id", id);
modelAndView.setViewName("detail");
return modelAndView;
}
其中value="/detail/{id}",中的{id}為佔位符表示可以映射請求為/detail/xxxx 的URL如:/detail/123等。
方法的參數@PathVariable(value="id") Integer id 用於將URL中佔位符所對應變數映射到參數id上,@PathVariable(value="id") 中value的值要和佔位符/{id}大括弧中的值一致。
在views中添加detail.jsp視圖,用於將獲取到的id值展示出來。視圖內容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
${id}
</body>
</html>
運行測試,請求URL地址 http://localhost:8080/SpringMVCLesson/helloworld/detail/123 ,結果如下:
可以看到已經正確的顯示了我們請求的id。
1.3.URL通配符映射:
我們還可以通過通配符對URL映射進行配置,通配符有「?」和「*」兩個字元。其中「?」表示1個字元,「*」表示匹配多個字元,「**」表示匹配0個或多個路徑。
例如:
「/helloworld/index?」可以匹配「/helloworld/indexA」、「/helloworld/indexB」,但不能匹配「/helloworld/index」也不能匹配「/helloworld/indexAA」;
「/helloworld/index*」可以匹配「/helloworld/index」、「/helloworld/indexA」、「/helloworld/indexAA」但不能匹配「/helloworld/index/A」;
「/helloworld/index/*」可以匹配「/helloworld/index/」、「/helloworld/index/A」、「/helloworld/index/AA」、「/helloworld/index/AB」但不能匹配 「/helloworld/index」、「/helloworld/index/A/B」;
「/helloworld/index/**」可以匹配「/helloworld/index/」下的多有子路徑,比如:「/helloworld/index/A/B/C/D」;
如果現在有「/helloworld/index」和「/helloworld/*」,如果請求地址為「/helloworld/index」那麼將如何匹配?Spring MVC會按照最長匹配優先原則(即和映射配置中哪個匹配的最多)來匹配,所以會匹配「/helloworld/index」,下面來做測試:
在HelloWorldController添加一個urlTest的action,內容如下:
@RequestMapping(value="/*", method = {RequestMethod.GET})
public ModelAndView urlTest(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("urltest");
return modelAndView;
}
在views文件夾中新加一個視圖urltest.jsp,為了和index.jsp做區別urltest.jsp的內容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
urlTest!
</body>
</html>
請求http://localhost:8080/SpringMVCLesson/helloworld/index查看結果:
可以看出映射的是index對應的action。
請求http://localhost:8080/SpringMVCLesson/helloworld/AAA查看結果:
可以看出映射的是urlTest對應的action。
1.4.URL正則表達式映射:
Spring MVC還支持正則表達式方式的映射配置,我們通過一個測試來展示:
在HelloWorldController添加一個regUrlTest的action,內容如下:
@RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", method = {RequestMethod.GET})
public ModelAndView regUrlTest(@PathVariable(value="name") String name, @PathVariable(value="age") Integer age){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name", name);
modelAndView.addObject("age", age);
modelAndView.setViewName("regurltest");
return modelAndView;
}
在views文件夾中新加一個視圖regurltest.jsp內容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
${name}-${age}
</body>
</html>
請求http://localhost:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-18查看結果:
請求http://localhost:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-Lilei查看結果(會發現找不到對應的action返回404):
2.限制action所接受的請求方式(get或post):
之前我們在HelloWorldController的index() action方法上配置的為@RequestMapping(value="/*", method = {RequestMethod.GET})我們把它改為@RequestMapping(value="/*", method = {RequestMethod.POST})再次請求http://localhost:8080/SpringMVCLesson/helloworld/index試一下:
這里可以看到結果映射到了urlTest這個action,這是因為我們在urlTest上配置的為@RequestMapping(value="/*", method = {RequestMethod.GET}),當index這個action映射不在符合時便映射到了urlTest。
我們也可以這樣配置@RequestMapping(value="/*", method = {RequestMethod.GET, RequestMethod.POST})表示該action可以接受get或post請求,不過更簡單的是不對method做配置則默認支持所有請求方式。
3.限制action所接受請求的參數:
我們可以為某個action指定映射的請求中必須包含某參數,或必須不包含某參數,或者某參數必須等於某個值,或者某參數必須不等於某個值這些限制。
3.1.指定映射請求必須包含某參數:
在HelloWorldController添加一個paramsTest的action,內容如下:
@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET})
public ModelAndView paramsTest(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("paramstest");
return modelAndView;
}
在views文件夾中新加一個視圖paramstest.jsp內容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
paramstest!
</body>
</html>
請求http://localhost:8080/SpringMVCLesson/helloworld/paramstest查看結果:
這里可以看到沒有找到paramsTest這個action結果還是映射到了urlTest這個action。