Skip to content

Commit 421ded7

Browse files
committed
customize error page, update readme
1 parent 3558570 commit 421ded7

8 files changed

Lines changed: 52 additions & 36 deletions

File tree

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,29 @@ SpringBlog is powered by many powerful frameworks and third-party projects:
1515
- [Pygments](http://pygments.org/) - Python syntax highlighter
1616
- [Jade4j](https://github.com/neuland/jade4j) - [Jade](http://jade-lang.com/) is an elegant template language
1717
- [Webjars](http://www.webjars.org/) - A client-side web libraries packaged into JAR files. A easy way to manage JavaScript and CSS vendors in Gradle.
18+
- [Redis](http://redis.io/) - A very powerful in-memory data cache server.
1819

1920
## Development
2021

21-
Make sure Gradle is installed in your machine. Try `gradle -v` command. Otherwise install in from [http://www.gradle.org/](http://www.gradle.org/).
22+
Before development, please install the following service software:
2223

23-
Before development, please install MySQL5+ first and edit `persistence.properties` according to your database configurations.
24+
- MySQL5
25+
- Configuration file: `src/main/java/persistence.properties`
26+
- Redis
27+
- [how-to-install-and-use-redis-on-ubuntu](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis)
28+
- Configuration file: `src/main/java/redis.properties`
29+
30+
And start MySQL and Redis server before running the application.
31+
32+
```
33+
# If you're using Ubuntu server
34+
sudo service mysql start
35+
sudo service redis_6379 start
36+
```
37+
38+
This is a Gradle project. Make sure Gradle is installed in your machine.
39+
Try `gradle -v` command. Otherwise install in from [http://www.gradle.org/](http://www.gradle.org/).
40+
I recommend you import the source code into Intellij IDE to edit the code.
2441

2542
```
2643
# Install artifacts to your local repository
@@ -30,4 +47,4 @@ Before development, please install MySQL5+ first and edit `persistence.propertie
3047
./gradlew jettyRun
3148
```
3249

33-
View `http://localhost:8080/SpringBlog` on your browser.
50+
View `http://localhost:8080/` on your browser.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,6 @@ buildscript {
127127
// It will cause "No Spring WebApplicationInitializer types detected on classpath"
128128
gretty {
129129
port = 8080
130-
contextPath = 'SpringBlog'
130+
contextPath = ''
131131
servletContainer = 'jetty9' //tomcat7 or tomcat8
132132
}

src/main/java/com/raysmond/blog/controllers/PostController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.raysmond.blog.controllers;
22

3+
import com.raysmond.blog.error.NotFoundException;
34
import com.raysmond.blog.models.Post;
45
import com.raysmond.blog.models.support.PostType;
56
import com.raysmond.blog.repositories.PostRepository;
@@ -36,6 +37,10 @@ public String archive(Model model){
3637
@RequestMapping(value = "{postId:[0-9]+}")
3738
public String show(@PathVariable Long postId, Model model){
3839
Post post = postService.getPost(postId);
40+
41+
if(post == null){
42+
throw new NotFoundException();
43+
}
3944
model.addAttribute("post", post);
4045
return "posts/show";
4146
}

src/main/java/com/raysmond/blog/error/CustomErrorController.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ public String generalError(HttpServletRequest request, HttpServletResponse respo
3030
if (requestUri == null) {
3131
requestUri = "Unknown";
3232
}
33-
34-
String message = MessageFormat.format("{0} returned for {1} with message {2}",
35-
statusCode, requestUri, exceptionMessage
36-
);
37-
38-
model.addAttribute("errorMessage", message);
33+
34+
model.addAttribute("statusCode", statusCode);
35+
model.addAttribute("requestUri", requestUri);
36+
model.addAttribute("exceptionMessage", exceptionMessage);
3937
return "error/general";
4038
}
4139

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.raysmond.blog.error;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
/**
7+
* Created by Raysmond on 9/27/15.
8+
*/
9+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
10+
public final class NotFoundException extends RuntimeException {
11+
12+
}

src/main/webapp/views/error/general.html

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extends ../layout/app
2+
3+
block title
4+
| Error
5+
= statusCode
6+
7+
block page_title
8+
h1 #{statusCode} #{exceptionMessage}
9+
p Request page: #{requestUri}
10+
p #{message}

src/main/webapp/views/fragments/alert.html

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)