Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import react component in jsp page Spring boot

I am creating Spring boot application and i want my frontend to be with React. The problem comes from the fact that i cannot find a way to properly integrate the react component in my jsp page.

Here is the declaration of the component:

ReactDOM.render(<App />, document.getElementById('root'));

I want to point out that my jsp page is inside WEB-INF directory and i can successfully redirect to it via a controller, but my import of the component does not work and does not throw an error!

I tried to import it by the element id:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Intellij is trash</title>
</head>
<body>
ala bala

<div id="root"></div>

<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
</body>
</html>

Am i doing something wrong or missed some configuration?

Thanks in advance for your help!

like image 791
Alex Vulchev Avatar asked Nov 17 '25 11:11

Alex Vulchev


1 Answers

After importing the React and ReactDOM scripts, import another script where you have written your component App. Assuming it is in App.js,

<script src="App.js"></script>

Now you need to render the component in the div with id="root". I will use babel to compile but you can do without it too. So after the import write:

<script type="text/babel">
ReactDOM.render(<App />, document.getElementById('root'));
</script>

Conclusion: Basically you need to import 4 files in this order: React, ReactDOM, Babel and your component.js files. Then you need to render the component in the div for it to work.

<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script src="App.js"></script>

<script type="text/babel">
    ReactDOM.render(<App />, document.getElementById('root'));
</script>
like image 95
PiNaKa30 Avatar answered Nov 19 '25 07:11

PiNaKa30



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!