A court in Japan has rejected a lawsuit against senior politicians' visits to the controversial Yasukuni war shrine.
Koizumi has visited the shrine four times since becoming leader
The suit claimed that visits by Prime Minister Junichiro Koizumi and Tokyo's governor violated the constitution.
The verdict may anger China, as the shrine commemorates Japanese war dead, among them convicted war criminals.
Relations between the Asian neighbours have already been strained in recent weeks by a series of feuds ignited by a dispute over Japan's wartime history.
Tuesday's verdict comes just days after Mr Koizumi met Chinese President Hu Jintao in an attempt to ease bilateral tensions.
In a sign that China, too, is anxious to appease Japan, police in Shanghai have detained 42 people in connection with the recent violent anti-Japanese protests, state media said on Tuesday.
Sixteen of these detainees will be charged with disturbing social order, the Shanghai Daily said.
The lawsuit was brought by a group of Japanese citizens and relatives of South Korean war dead, who argued that the annual visits to the Yasakuni shrine violated the separation of state and religion enshrined in Japan's constitution.
Built in 1869 to honour victims of the Boshin Civil War
Now venerates the souls of 2.5m of Japan's war dead
Those enshrined include 14 Class A war criminals
Each of the 1,047 plaintiffs had sought 30,000 yen ($284) in compensation for damages from Mr Koizumi and Tokyo Governor Shintaro Ishihara.
But government lawyers said the visits were private in nature, and therefore did not violate the country's laws.
Mr Ishihara, known for his outspoken views, said he was pleased with the decision.
"As a Japanese, I go to Yasukuni Shrine to express my respect and condolences for the war dead," he said in a statement. "I consider this ruling to be a very natural decision."
He then went further, telling Reuters news agency that he thought Japan should station troops on a chain of disputed islands in the East China Sea known as the Senkaku islands in Japan and the Diaoyu islands in China.
"We should send our military there and if Chinese activists or North Korean agents intrude and don't listen to orders to stop, then we should sink them," Mr Ishihara told Reuters.
The Yasukuni shrine is dedicated to the souls of 2.5 million Japanese who died in past wars, including convicted World War II criminals.
Official trips there are seen by Japan's critics as a sign that Tokyo does not sufficiently regret its brutal conquest of the region in the 1930s and 40s.
A group of Japanese lawmakers visited the shrine last Friday, amid growing tensions between Tokyo and Beijing.
Relations began to deteriorate two weeks ago, when Tokyo approved a set of controversial history textbooks which critics say do not acknowledge the extent of Japanese wartime brutality.
Since then there have been a series of rows over a disputed section of the East China Sea and Japan's attempts to secure a permanent UN Security Council seat.
In a move designed to ease tensions, Mr Koizumi made an unusual public apology for Japan's wartime aggression in a speech last weekend at a regional summit in Jakarta.