对于想了解如何通过SpringMVC在JSP中包含js和CSS的读者,本文将提供新的信息,我们将详细介绍springmvcjsp,并且为您提供关于asp.net-mvc-4–如何在asp.netMVC
对于想了解如何通过Spring MVC在JSP中包含js和CSS的读者,本文将提供新的信息,我们将详细介绍spring mvc jsp,并且为您提供关于asp.net-mvc-4 – 如何在asp.net MVC4查看页面中包含javascript代码?、Java Spring MVC在同一个JSP中获取/发布、JSON – Spring MVC:如何将json数据发布到spring MVC控制器、Spring Boot MVC在JSP中不使用模型值的有价值信息。
本文目录一览:- 如何通过Spring MVC在JSP中包含js和CSS(spring mvc jsp)
- asp.net-mvc-4 – 如何在asp.net MVC4查看页面中包含javascript代码?
- Java Spring MVC在同一个JSP中获取/发布
- JSON – Spring MVC:如何将json数据发布到spring MVC控制器
- Spring Boot MVC在JSP中不使用模型值
如何通过Spring MVC在JSP中包含js和CSS(spring mvc jsp)
我想在我的jsp中包含js和css文件,但我不能这样做。我是Spring MVC概念的新手。很长一段时间以来,我一直在研究同一个主题。我的索引页面是这样的
<!DOCTYPE html><html><head><link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style.css"/><script type="text/javascript" src="${pageContext.request.contextPath}/LoginPageScrip.js"></script><style type="text/css">body { background-image: url("LoginPageBackgroundImage.jpg");}</style></head><body > <h6>Please login in google Chrome</h6> <h1 align="center">Welcome to my Twitter Clone</h1> <div> <form method="post" action="LoginForExistingUser" onsubmit="return Validate(this);"> <fieldset> <legend align="center">Login</legend> <div> <div>User Name</div> <div> <INPUT type="text" name="userName"> </div> </div> <div> <div>Password</div> <div> <INPUT type="password" name="password"> </div> </div> <div> <div> <INPUTtype="submit" name="submit" value="Login"> </div> </div> <i align="center">New User? <a href="signup.html"><u>Signup</u></a></i> </fieldset> </form> </div></body> </html>
我的spring-dispatcher-servlet.xml就是这样。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.csc.student" /> <mvc:annotation-driven/> <!--<bean id="HandlerMapping"/>--> <!-- <bean name="/welcome.html"/> --> <bean id="viewResolver"> <property name="prefix"> <value>/WEB-INF/</value> </property> <property name ="suffix"> <value>.jsp</value> </property> </bean></beans>
我的控制器就是这样。
package com.csc.student; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class StudentInfoController { @RequestMapping(value = "/indexPage.html", method = RequestMethod.GET) public ModelAndView getAdmissionFrom() { ModelAndView model = new ModelAndView("indexPage"); return model; } }
有人可以帮我吗?我正在努力奋斗,但没有任何解决方案。我已经将我的js和css文件保存在WEB-INF文件夹中。
答案1
小编典典首先,您需要像下面这样在dispatcher-servlet文件中声明资源:
<mvc:resources mapping="/resources/**" location="/resources/folder/" />
带有网址映射/ resources / **的任何请求都将直接查找/ resources / folder /。
现在在jsp文件中,您需要像这样包含css文件:
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
同样,您可以包含js文件。
希望这能解决您的问题。
asp.net-mvc-4 – 如何在asp.net MVC4查看页面中包含javascript代码?
@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryui") <script type="text/javascript"> $(function () { $("#sections").tabs(); //here i am getting error that Object[object object] has not method tabs }); </script> <div id="sections"> <ul> <li><a href="#section-1">section-1 </a></li> <li><a href="#section-2">section-2 </a></li> </ul> <div id="section-1"> section-1 content............ </div> <div id="section-2"> section-2 content............ </div> </div>
提前致谢……
解决方法
@Scripts.Render("~/bundles/jquery") @RenderSection("scripts",required: false) </body>
所以jQuery包已经包含在你的页面中.您不应该在视图中第二次包含它.你只需要〜/ bundles / jqueryui包:
@Scripts.Render("~/bundles/jqueryui") <script type="text/javascript"> $(function () { $("#sections").tabs(); //here i am getting error that Object[object object] has not method tabs }); </script> <div id="sections"> <ul> <li><a href="#section-1">section-1 </a></li> <li><a href="#section-2">section-2 </a></li> </ul> <div id="section-1"> section-1 content............ </div> <div id="section-2"> section-2 content............ </div> </div>
更新:
以下是视图结构可能如下所示的完整示例:
@{ Layout = null; } <!DOCTYPE html> <html> <head> <Meta name="viewport" content="width=device-width" /> <title>Foo</title> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" /> </head> <body> <div id="sections"> <ul> <li><a href="#section-1">section-1 </a></li> <li><a href="#section-2">section-2 </a></li> </ul> <div id="section-1"> section-1 content............ </div> <div id="section-2"> section-2 content............ </div> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryui") <script type="text/javascript"> $("#sections").tabs(); </script> </body> </html>
Java Spring MVC在同一个JSP中获取/发布
@RequestMapping(value = "/add",method = RequestMethod.GET) public String add(Model model) { return "add"; } @RequestMapping(value = "/add",method = RequestMethod.POST) public String added(@RequestParam("name") String name,Model model) { City city = new City(); city.setCity(name); service.addCity(city); return "add"; }
这是我的JSP视图..这只是为了添加…这是add.jsp..so它回发给自己
<form method="post" action="/spring/krams/edit/add"> Linna nimi <input type="text" name="name"> <input type="submit" value="Test" name="submit" /> </form>
我希望JSP文件发生变化,以便当我将其发布到此文件时,它会说…“添加了城市”.那可能吗?
什么关于更新城市?
@RequestMapping(value = "/update",method = RequestMethod.POST) public String updated(@RequestParam("city") int city_id,@RequestParam("text") String name,Model model) { service.updateCity(name,city_id); return "update"; }
这里没有对象?
解决方法
@RequestMapping(value = "/add",Model model) { City city = new City(); city.setCity(name); service.addCity(city); model.addAttribute("city",city); return "add"; }
在JSP中,您可以检查属性city是否为null(标记为< c:if />).如果它不是null,那是因为它刚刚被添加到模型中,所以你可以显示你想要的任何东西. ${city.city}它只是一个JSTL表达式,它访问city属性,然后调用getter来访问该属性的city属性:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <c:if test="${city != null}"> CITY <c:out value="${city.city}" /> ADDED </c:if>
UPDATE
如果根据更新/创建操作需要不同的消息,则可以执行以下操作:(在示例中,当id param不为null时进行更新,因为id是要更新的城市的标识符)
@RequestMapping(value = "/add",method = RequestMethod.POST) public String added(@RequestParam(value="id",required=false) String id,@RequestParam("name") String name,Model model) { City city; String operation; if(id== null){ //create operation city = new City(); operation = "CREATE"; }else{ //update operation city = service.findCity(id); operation = "UPDATE"; } city.setCity(name); service.saveCity(city); //save or update model.addAttribute("city",city); model.addAttribute("operation",operation); //add operation param return "add"; }
在JSP中你可以做到:
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %> <c:if test="${operation == 'CREATE'}"> <c:if test="${city != null}"> CITY <c:out value="${city.city}" /> ADDED </c:if> <c:if test="${operation == 'UPDATE'}"> CITY <c:out value="${city.city}" /> UPDATED </c:if> </c:if>
JSON – Spring MVC:如何将json数据发布到spring MVC控制器
我在将jsp数据从jsp发布到控制器时遇到问题.我每次尝试都会收到ajax错误错误请求.我是JSON的新手,我真的不知道我做错了什么.我搜索并尝试了一些我可以在这个网站找到的样本,但我仍然有问题.
在我的控制器中:
@RequestMapping (method = RequestMethod.POST,headers ={"Accept=application/json"},value = "/form")
public String postJournalEntry (@RequestParam ("json") String json,Model model) {
System.out.println(json);
return "successfullySaved";
}
在我的jsp中:
$("#btnPostGlEntry").click(function () {
var glEntries = '{"glEntries":[{"generalLedgerId":"1"},{"accountId":"4"},{"amount":"344.44"},{"description":"Test Entry"},{"debit":"Yes"}]}';
$.ajax({
type: "POST",contentType: "application/json",dataType: "json",url: contextpath + "/generalLedger/journalEntries/form",data : JSON.stringify(glEntries),success: function(data) {
alert("Success!!!");
},error: function (jqXHR,textStatus,errorThrown) {
alert(jqXHR + " : " + textStatus + " : " + errorThrown);
}
});
});
注意:我甚至不确定我的控制器中的功能是否正确.我认为我的控制器和我的ajax是错误的.请帮忙.
.... method(@RequestBody MyClass data){ ... }
但是,如果您希望您的方法接受JSON作为String而不是这样做:
.... method(@RequestBody String json){ ... }
所以,基本上,如果你发布JSON,这意味着JSON不是一个参数,它是请求的主体.最后你必须使用@RequestBody注释,而不是@RequestParam.
你可以在这里找到Spring Mvc和JSON的精彩视频教程:sites.google.com/site/upida4j/example
Spring Boot MVC在JSP中不使用模型值
我正在开发Spring Boot MVC应用程序。我的问题是,当视图出现时,它不会解析模型中的值。
application.properties
spring.application.name=test-mvcserver.context-path=/test-mvc
设定档
@Configuration@EnableWebMvcpublic class ApplicationConfigurerAdapter extends WebMvcConfigurerAdapter{ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/pages/"); resolver.setSuffix(".jsp"); return resolver; }}
应用
@SpringBootApplicationpublic class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
控制者
@Controller@RequestMapping("/view")public class TestController { @RequestMapping("/greeting") public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, ModelMap model) { model.addAttribute("name", name); return "greeting"; }}
greeting.jsp
<html> <head><title>Hello world Example</title></head> <body> <h1>Hello ${name}, How are you?</h1> </body></html>
来自http:// localhost:8080 / test-mvc / view / greeting?name =
Mikey的输出:
您好$ {name},您好吗?
我也尝试过返回ModelAndView。我不确定为什么不将“ Mikey”放在哪里${name}
。
更新资料
每次我向JSP页面添加一个标记库或任何东西时,它都会显示页面源。它不会呈现页面。下面是我的pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version></parent><properties> <hibernate.version>5.0.5.Final</hibernate.version> <postgres.version>9.4.1208</postgres.version></properties><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>org.apache.taglibs</groupId> <artifactId>taglibs-standard-impl</artifactId> <version>1.2.5</version> </dependency></dependencies>
答案1
小编典典在您的中添加以下依赖项pom.xml
:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
您可能需要添加以下依赖项以避免javax.servlet.ServletException: java.lang.NoSuchMethodError:org.apache.el.lang.ELSupport.coerceToType
。如果您不添加它,scope=required
那么 tomcat-
embed-el-8.0.32.jar 将被放置在您的Tomcat 8.0\webapps\AppName\WEB-INF\lib
文件夹中,并会导致上述错误。如果您通过IDE或mvn Spring-boot:run
命令运行您的应用,则无需添加以下依赖项。
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-el</artifactId> <scope>provided</scope> </dependency>
然后访问-http:// localhost:8080 / AppName / view / greeting?name =
Mikey
检查此仓库以获取完整的源代码。
更新: 这个问题JSP示例的Tomcat的战争部署工作不因“Tomcat的嵌入-EL”依赖固定在spring启动-
1.4.0.M2
今天关于如何通过Spring MVC在JSP中包含js和CSS和spring mvc jsp的讲解已经结束,谢谢您的阅读,如果想了解更多关于asp.net-mvc-4 – 如何在asp.net MVC4查看页面中包含javascript代码?、Java Spring MVC在同一个JSP中获取/发布、JSON – Spring MVC:如何将json数据发布到spring MVC控制器、Spring Boot MVC在JSP中不使用模型值的相关知识,请在本站搜索。
本文标签: