A simple blog created during Java/OOP course in university.
Username/password auth, admin can add/edit/delete blog posts, users (after registration) can add comments, vote (+1/-1) for posts and comments.
Can be useful for someone who is learning the techonologies/frameworks used here.
The project is deployed here http://109.120.169.99:8080/blog, admin username/password — admin/pass123, the same password for other users, Alice/Bob/User1-5.
Features:
/users/username
url or link on each username in the comments section), and small avatar image is also displayed in each comment.Requirements:
#
Obtain the project source files (git clone
or download and extract zip archive).
Modify configuration if needed.
Spring profiles are used to apply different configuration files for development (dev), real server (prod) and tests (test).
src\main\resources
directory) contain directory path for user uploaded files (such as avatar images).Example (uploading-prod.properties):
uploading.dirpath=/var/blog/upload/
src\main\resources directory
) contain database configuration: database driver, address, name, username/password, ...<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/blog" /> <property name="username" value="BlogDbUser" /> <property name="password" value="BlogDbPassword" /> ... <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> ...
The database must contain schema with the specified name (blog by default) and user with the specified name/password.
If you want to use another database instead of MySQL you will need to modify driverClassName
, url
and hibernate.dialect
values (highlighted above) and supply the JDBC driver (such as by adding Maven dependency to pom.xml).
This line specifies SQL files that will be executed when database is created (by default it fills database with some demo data and creates a table for Spring Security “remember me” feature)
<entry key="hibernate.hbm2ddl.import_files" value="/security-tables.sql,/dummy-data.sql" />
Also note this line:
<entry key="hibernate.hbm2ddl.auto" value="create"/>
It will drop and create database tables each time you deploy the project.
See Hibernate, JDBC and Spring documentation for more information about possible configuration parameters.
By default the same datasource-dev.xml file is used for both dev and prod profiles. If needed, you can create a separate file (datasource-prod.xml) and modify <beans profile="prod">
node in database.xml to use it:
<beans profile="prod"> <import resource="classpath:/datasource-prod.xml"/> </beans>
Run Maven verify
goal. This will download all dependencies, run JUnit tests and build WAR file. Check Maven output to see if all tests and build are completed successfully.
Set -Dspring.profiles.active
system property to specify profile that will be used. If not set the default profile is dev.
For example in <tomcat_dir>/bin/setenv.sh
:
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=prod"
Deploy the WAR file to Tomcat (or other web server).
Go to http://your-server-address/blog
(if deployed with default Tomcat settings) to see if it is working.
If for debug, you can run mvn tomcat7:run-war
to launch server without installing tomcat yourself. Of course, you need to launch mysql service yourself : )
加入了一个依赖包,请先执行
cd libs && sh installLibsToMaven.sh
安装依赖包到本地 maven 缓存,再通过 mvn tomcat7:run-war
运行程序,或直接打包到 tomcat 里运行程序