Created
February 9, 2015 11:28
-
-
Save warmuuh/985eb68506c33bcb5c32 to your computer and use it in GitHub Desktop.
Serverside Rendering in Spring with React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsxViewResolver implements ViewResolver { | |
@Setter String prefix = "static/"; | |
@Setter String suffix = ".js"; | |
@Setter String indexFile = "/templates/index.html"; | |
private JReact renderer; | |
private MessageFormat format; | |
@PostConstruct | |
public void init() throws IOException{ | |
renderer = new JReact(true); | |
renderer.addRequirePath(prefix); | |
ClassPathResource res = new ClassPathResource(indexFile); | |
format = new MessageFormat(IOUtils.toString(res.getInputStream())); | |
} | |
@Override | |
public View resolveViewName(String viewName, Locale locale) throws Exception { | |
return new JsxView(renderer, viewName + suffix, format); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AllArgsConstructor | |
public class JsxView extends AbstractView { | |
private JReact renderer; | |
private String viewName; | |
private MessageFormat indexTemplate; | |
@Override | |
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, | |
HttpServletResponse response) throws Exception { | |
String content = renderer.renderToString("./"+viewName, model); | |
renderer.reset(); | |
IOUtils.write(indexTemplate.format(new String[]{content}), response.getOutputStream()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@SpringBootApplication | |
class HomeController { | |
public static void main(String[] args) { | |
SpringApplication.run(HomeController.class, args); | |
} | |
@Bean | |
public JsxViewResolver resolver() { | |
return new JsxViewResolver(); | |
} | |
@RequestMapping("/") | |
ModelAndView showDashboard() { | |
ModelAndView mav = new ModelAndView("test"); | |
mav.addObject("server", true); | |
return mav; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Test = React.createClass({ | |
getDefaultProps: function(){ | |
return {server: false} | |
}, | |
render: function() { | |
return ( | |
<div> | |
Rendered from: {this.props.server ? 'server' : 'client'} | |
</div> | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment