Ⅰ 如何用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。