本文目錄一覽:
- 1、jsp和Java後台數據如何交互
- 2、JSP如何調用java代碼
- 3、jsp如何調用java類
- 4、java開發的信息系統里,jsp與java文件是怎麼傳遞數據的啊?
- 5、jsp 怎麼用java和jsp傳值
- 6、spring 中如何實現jsp與java的交互
jsp和Java後台數據如何交互
%
String path = request.getContextPath();
%
獲取jsp所在工程的名稱
var ids = new Array();
$.ajax({
type : “POST”,
contentType : ‘application/json’,
url : ‘%=path%/ui/product/havePsmPackage’,
data:JSON.stringify(ids),
async :false,
dataType : “json”,
success : function(data) {
},
error:function(){
}
});
1、前台如果傳的是一個集合,後台可以使用參數 @RequestBody ListString ids 來接收
2、如果前台是這種傳值方式 data:{“name”:name,”id”:id},
那後台可以通過創建一個欄位名稱對應的實體類來接收
或者使用String name = request.getParameter(“name”)的方式來接收
3、如果ajax選擇的GET方法,那後台方法的欄位名稱和url的入參名稱保持一致就能接收到數據了
4、window.location.href=”%=path%/ui/psmpackage/toPsmPackageList”;
這是跳轉到新頁面的方法
@RequestMapping(value = “/toPsmPackageList”)
public String toPsmPackageList(HttpServletRequest request) {
return “psmPackage/psmPackageList”;
}
這是後台的接受方式,返回的是對應jsp的文件夾和文件名
頁面跳轉的方法不需要@ResponseBody註解,而獲取返回值的方法則一定要加,不然獲取不到返回值
5、window.open(url); 可以在瀏覽器上新開一個頁面。對應的是window.close();
6、如何將數據帶到新增的頁面
第一:第一個頁面通過
window.location.href=”%=path%/ui/product/condition?lineCode=”+lineCode+”typeCode=”+typeCode;傳值
在新頁面可以通過var lineCode = ‘%=(String)request.getParameter(“lineCode”)%’這種方式來取值
第二:後台代碼這樣處理request.setAttribute(“product”, result);
input name=”id” type=”text” value=”${product.id}” /
${}來取值。
JSP如何調用java代碼
首先,在服務端定義了一個類和靜態方法:
public class Test {
public static String sayHello(String name){
return “hello, ” + name;
}
}
注意:必須是公開的靜態方法。
ajax分為同步、非同步調用,如果沒有傳遞迴調函數,則是同步調用;
如果傳遞迴調函數,則是非同步調用。
同步調用
script type=”text/javascript”
……
try{
var obj = Test.sayHello(‘world!’);
……
}catch(e){
//e.error 錯誤碼
//e.message 錯誤描述
}
……
/script
不傳遞迴調函數,以try…catch來處理錯誤。
在同步調用模式下,如果有網路錯誤、業務邏輯錯誤等,會拋出一個異常。
非同步調用
script type=”text/javascript”
……
Test.sayHello(‘world!’,
function(obj){ //success callback function
……
},
function(error, message){ //fail callback function
}
);
……
/script
傳遞迴調函數作為參數。如果只有一個回調函數, 則默認是成功的回調函數。
jsp如何調用java類
jsp調用java類的步驟:
1.新建一個項目,在src文件夾下添加一個包:如:tianaoweb.com;
2.再在包中添加一個類:如
package com;
public class test {
public String sd(){
return “sd”;
}
}
3.在默認的首頁index.jsp(當然也可以自己新建一個jsp文件)的開頭引入
%@ page import= “tianaoweb.com.* “%
4.在body /body中添加相應的java代碼片:
如:
%
String str;
test te=new test();
%
%=te.sd() %
java開發的信息系統里,jsp與java文件是怎麼傳遞數據的啊?
jsp與java文件傳遞數據可以使用Servlet類來傳遞,jsp將數據存入到request對象中,Servlet類獲取這個request對象,並將數據取出。
示例代碼如下:
JSP代碼:%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%
!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
html
head
titleDemo/title
/head
body
form action=”/demoServlet” method=”post”
input type=”text” name=”name”/
input type=”submit” value=”提交”/
/form
/body
/html
Servlet代碼:
public class DemoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter(“name”);//獲取jsp頁面輸入的參數
System.out.println(name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
表單提交之後,調用Servlet類的方法,通過request對象獲取jsp頁面傳入的參數值,實現數據的傳遞。
jsp 怎麼用java和jsp傳值
1.JSP傳值給Servlet
JSP傳值給Servlet有幾種形式:Form表單傳值,url傳值,其他方式傳值
a、form表單傳值:
JSP頁面有:input type=”radio” name=”staffdepartment” value=”1″ id=”department1″ /,將department的id傳到Sevlet中,在程序中如下:ServletRequest request;Stringstaffdepartment=request.getParameter(“staffdepartment”);可獲取jsp傳的department,但要獲取id還要進行轉化:int int_staffdepartment=Integer.parseInt(staffdepartment);
b、url傳值
比如這裡的 a標籤的 href屬性與 form標籤的 action屬性的值 “JspServlet?action=toServlet”,在 servlet同樣用 request.getParameter(“action”)獲取;
c、Java代碼傳值
java片段代碼,servlet只能接到 session.setAttribute(“testSession”,”Hello session”)的內容,而接不到 request的內容。在 servlet里用 request.getSession().getAttribute(“testSession”)獲取 session內容。
Servlet傳值給Jsp
具體實現如下,java代碼:
String a= “abccdefg “;
request.setAttribute( “ValueA “,a);
request.getRequestDispatcher( “網址/jsp頁面 “).forward(request,response);
jsp頁面:
%
String s =(String)request.getAttribute( “ValueA “);
%
jsp頁面就可以取出Servlet的值。
spring 中如何實現jsp與java的交互
spring中利用mvc框架就可以實現jsp和java交互了。
以下用spring mvc輸出hello world為例來說明:
一、項目依賴的jar包:
1.Spring框架依賴的jar包:
日誌:commons-logging-1.1.3.jar;
JSTL支持:jakarta-taglibs-standard-1.1.2中的jstl.jar和standard.jar;
2.Spring的jar包:
spring-framework-3.2.5.RELEASE/libs中的jar包(這裡為了方便我直接把全部都複製過去了);
把以上的jar包全部複製到項目的WebContent/WEB-INF/lib目錄中。
二、在/WEB-INF中添加web.xml文件,文件內容如下:
?xml version=”1.0″ encoding=”UTF-8″?
web-app xmlns:xsi=”” xmlns=”” xmlns:web=”” xsi:schemaLocation=” ” id=”WebApp_ID” version=”3.0″
display-nameSpringMVCLesson/display-name
servlet
servlet-nameSpringMVCLesson/servlet-name
servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class
init-param
param-namecontextConfigLocation/param-name
param-valueclasspath:springservlet-config.xml/param-value
/init-param
load-on-startup1/load-on-startup!– load-on-startup必須放在最後 —
/servlet
!– Spring MVC配置文件結束 —
servlet-mapping
servlet-nameSpringMVCLesson/servlet-name
url-pattern//url-pattern
/servlet-mapping
/web-app
三、springservlet-config.xml文件配置:
在項目中新建一個resources的Source Folder文件夾,並添加springservlet-config.xml文件。
?xml version=”1.0″ encoding=”UTF-8″?
beans xmlns=””
xmlns:xsi=””
xmlns:p=””
xmlns:context=””
xmlns:util=””
xmlns:mvc=””
xsi:schemaLocation=”
”
!– 默認的註解映射的支持 —
mvc:annotation-driven/
!– 如果當前請求為「/」時,則轉發到「/helloworld/index」 —
mvc:view-controller path=”/” view-name=”forward:/helloworld/index”/
!– 靜態資源映射 —
mvc:resources mapping=”/js/**” location=”/WEB-INF/js/” /
mvc:resources mapping=”/css/**” location=”/WEB-INF/css/” /
mvc:resources mapping=”/fonts/**” location=”/WEB-INF/fonts/” /
mvc:resources mapping=”/plugins/**” location=”/WEB-INF/plugins/” /
mvc:resources mapping=”images/**” location=”/WEB-INF/images/” /
!– 當上面要訪問的靜態資源不包括在上面的配置中時,則根據此配置來訪問 —
mvc:default-servlet-handler/
!– 開啟controller註解支持 —
!– use-default-filters=”false” 只掃描指定的註解 —
context:component-scan base-package=”com.demo.web.controllers” use-default-filters=”false”
context:include-filter type=”annotation” expression=”org.springframework.stereotype.Controller”/
/context:component-scan
!– 視圖解析器 —
bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”
property name=”viewClass” value=”org.springframework.web.servlet.view.JstlView”/
property name=”contentType” value=”text/html”/
property name=”prefix” value=”/WEB-INF/views/”/
property name=”suffix” value=”.jsp”/
/bean
/beans
mvc:annotation-driven/ 開啟註解映射支持,它是為了簡化配置的縮寫形式,它相當於以下2個配置:
bean class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”/
bean class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter”/
由於我們在web.xml文件裡面配置的是攔截所有的請求到該servlet,所以我們在這裡要對靜態資源文件映射作一下配置,否則請求這些資源文件會返回404:
!– 靜態資源映射 —
mvc:resources mapping=”/js/**” location=”/WEB-INF/js/” /
mvc:resources mapping=”/css/**” location=”/WEB-INF/css/” /
mvc:resources mapping=”/fonts/**” location=”/WEB-INF/fonts/” /
mvc:resources mapping=”/plugins/**” location=”/WEB-INF/plugins/” /
mvc:resources mapping=”images/**” location=”/WEB-INF/images/” /
!– 當上面要訪問的靜態資源不包括在上面的配置中時,則根據此配置來訪問 —
mvc:default-servlet-handler/
開啟Controller註解支持,並配置只掃描指定包下面的Controller:
context:component-scan base-package=”com.demo.web.controllers” use-default-filters=”false”
context:include-filter type=”annotation” expression=”org.springframework.stereotype.Controller”/
/context:component-scan
配置視圖解析器,並指定視圖所在的文件夾:
bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”
property name=”viewClass” value=”org.springframework.web.servlet.view.JstlView”/
property name=”contentType” value=”text/html”/
property name=”prefix” value=”/WEB-INF/views/”/
property name=”suffix” value=”.jsp”/
/bean
添加HelloWorldController,在項目中新建一個web的Source Folder文件夾,並在文件夾下面添加com.demo.web.controllers包,在包中添加一個HelloWorldController類,類中內容如下:
package com.demo.web.controllers;
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
@RequestMapping(value = “/helloworld”)
public class HelloWorldController {
@RequestMapping(value=”/index”, method = {RequestMethod.GET})
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject(“message”, “Hello World!”);
modelAndView.setViewName(“index”);
return modelAndView;
}
}
其中@Controller 註解把該class指定為controller,controller 上的@RequestMapping 註解的 value值指定該controller所映射的請求。
方法上的@RequestMapping 註解指定該方法為一個action,value 值指定該action所映射的請求,method 中的RequestMethod.GET指定該action只接受get請求。
ModelAndView 中的setViewName指定了該action所對應的視圖名稱,解析視圖時會在springservlet-config.xml文件指定的視圖文件夾中尋找對應的視圖。
添加視圖,在項目/WEB-INF文件夾下新建一個views文件夾,並在views中添加index.jsp視圖,視圖內容如下:
%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%
!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “”
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″
titleInsert title here/title
/head
body
${message}
/body
/html
運行項目輸出結果:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/303495.html