How To Integrate React in JSP Application : React와 JSP 연동

How To Integrate React in JSP Application

Hello folks!

Today, I want to share a problem that I was having at work. I have a very legacy application written in JSP using scriplets, tiles and what not they were using in thier stone age era. I wanted to use React for a set of UIs connected via routers. So I created the entire app in React which can be used in the place of certain set of JSPs.

THE PROBLEM

Now the problem was that I had to embed the ReactJS application into the JSP application using the same construct and framework. There were lot of JSPs with different URLs and different parameters passed from servlet context.

If you are wondering how to acheive this in React and Integrating into JSP, please read on.

THE SOLUTION

There are 2 things you need to do to achieve this. React Fix and Servlet Container Fix (tomcat application in my perspective). Lets look at them.

React Fix

The change on react side is to provide the “homepage” property in package.json to enable react build script to consider the same. In my side, the application will be hosted under a folder named “/web/br” hence my package.json looks like below:

package.json
package.json

Next things to do in React is to provide a basename for all your routes so it start from the JSP you will put the react code in. In my case I am putting react within a jsp named “page2.jsp” under the folder “/web/br” hence my main App.js looks like below:

 

App.js
App.js

Now, lets talk about how we can enable the passing of URL parameters from JSP level to React App. When we hit a hit with a url parameters like “?param1=value1” and this page contains a react app, the url params will be passed to props.location.search as it is. The auto parsing into name value is removed from react router hence not automatically possible. However there is a simpler way to get it done. I prefer using URLSearchParams only.

const params = URLSearchParams(props.location.search)

and then you can get the params using the params object. Please refer API guide for the same. [URLSearchParams]

Done with ReactJS fix.

Tomcat Fix

Now, to be able to use the ReactJS complete framework with router, redux and state we need to tell our container to route all requests for a specific pattern to the same JSP. Below is my web.xml for the war project.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

web.xml
web.xml

It tells the container to route all requests “/br/*” to my servlet which is a jsp embedding the react app.

The last part is simple. Embed the <div> tag for holding react app and set proper width and height for your application.

Well that all for the solution. It works like a charm. Hoping this would help you as well!

Let me know if this worked for you or you employ any other solution.

Please leave a comment and happy hacking!

 

[출처] http://technology.toraveabout.com/integrate-react-in-jsp-application/

 

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
146 Connect to MSSQL Server in JPA file 졸리운_곰 2020.01.24 85
145 Tomcat 및 MySQL 관련 한글 인코딩 문제 졸리운_곰 2020.01.23 88
144 전자정부 표준 프레임워크 egov bootstrap 부트스트랩 적용 file 졸리운_곰 2020.01.15 2130
143 JHIPSTER로 빠르게 SPRING BOOT, ANGULAR PROJECT 세팅하기 file 졸리운_곰 2019.12.31 246
142 Spring Boot와 AngualrJS를 조합한 코드 자동 생성 도구(scaffolding) file 졸리운_곰 2019.12.31 88
141 spring security 파헤치기 (구조, 인증과정, 설정, 핸들러 및 암호화 예제, @Secured, @AuthenticationPrincipal, taglib) file 졸리운_곰 2019.04.06 276
140 DAO vs DTO(=VO) 개념 알아보기 졸리운_곰 2019.01.04 217
139 DAO / VO / DTO란? 졸리운_곰 2019.01.04 272
138 JSP get 전송을 수신페이지에서 hidden으로 받아 javascript에서 활용 졸리운_곰 2019.01.03 60
137 JSP와 자바스크립트간의 정보 전송방법 file 졸리운_곰 2019.01.03 95
136 Intellij 에서 spring boot + jsp 설정 file 졸리운_곰 2018.12.01 143
135 How to Setup Spring-Boot with ReactJS and Webpack 스프링부트와 ReactJS와 webpack 패키징 file 졸리운_곰 2018.10.28 75
134 Use React and Spring Boot to Build a Simple CRUD App 기본적인 스프링부트와 React.js의 CRUD 기능 구현 file 졸리운_곰 2018.10.28 171
133 Bootiful Development with Spring Boot and React 스프링 부트와 리엑트.js의 연동 file 졸리운_곰 2018.10.28 121
132 [Spring] 스프링 비밀번호 암호화 졸리운_곰 2018.10.08 2280
131 cos.jar로 파일 업로드 하기 졸리운_곰 2018.09.11 207
130 React.js and Spring Data REST : react.js 자바 스프링 연동 file 졸리운_곰 2018.09.11 2106
» How To Integrate React in JSP Application : React와 JSP 연동 file 졸리운_곰 2018.09.11 1516
128 [CKEditor] JSP에서 사용하기 졸리운_곰 2018.09.06 124
127 [JSP]톰캣 서버 IntelliJ 구동,JSP 간단한 예제 실행 file 졸리운_곰 2018.09.05 309
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED