为什么我的Spring网络应用程序项目没有进入主页?

时间:2014-09-12 07:52:04

标签: java spring spring-mvc web

这是我使用的控制器

@Controller

public class ProjectController {

    @Autowired

    private ProjectService ProjectService;

    @Autowired
    public ProjectController(ProjectService ProjectService){
        this.ProjectService = ProjectService;
    }


        @RequestMapping({"/","/find"})
        public String getPersonList(@RequestParam("regNo") String regNo, ModelMap model) {  

            model.addAttribute("ProjectList", ProjectService.getStudentProject(regNo));  

            return "find";  
        }  
}

我在WEB-INF / jsp文件夹下有一个名为find.jsp的页面。但当我点击localhost:8080 /发现我

HTTP状态400错误

以下是我的dispatcher-servlet.xml,其中我已经制作了映射

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:beans="http://springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/context/spring-context.xsd       
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/task/spring-task.xsd">

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.MyAppName.www" />

    <!-- Factory bean that creates the Mongo instance -->
    <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
        <property name="host" value="localhost" />
    </bean>

    <!-- MongoTemplate for connecting and quering the documents in the database -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="MyApp" />
    </bean>

    <!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp"
          p:suffix=".jsp" /> 

    <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>

    <bean class=
    "org.springframework.web.servlet.view.tiles2.TilesConfigurer">
  <property name="definitions">
    <list>
      <value>/WEB-INF/views/views.xml</value>
    </list>
  </property>
</bean>    

</beans>

这是web.xml的外观

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">

  <display-name>Spring With MongoDB Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <welcome-file-list>
       <welcome-file>/WEB-INF/find.jsp</welcome-file>
    </welcome-file-list> 
</web-app>

这就是我的服务器日志的样子。我正在使用Tomcat 7.0

Sep 12, 2014 1:24:21 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 12, 2014 1:24:21 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Sep 12, 2014 1:24:21 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 502 ms

我使用的网址是localhost:8080 / find.jsp,我收到404错误。

我在views.xml的定义标记中也有问题

<!DOCTYPE tiles-definitions PUBLIC
      "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
      "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">

<tiles-definitions>
   <definition name="template" template="/WEB-INF/jsp/main_template.jsp">
     <put-attribute name="top"
                    value="/WEB-INF/jsp/tiles/header.jsp" />
     <put-attribute name="side"
                    value="/WEB-INF/jsp/tiles/footer.jsp" />

   </definition>

    <definition name="home" extends="template">Define home tile
             <put-attribute name="content" value="/WEB-INF/jsp/find.jsp" />
    </definition>
</tiles-definitions>

1 个答案:

答案 0 :(得分:0)

ProjectService属性更改为小写。    删除@RequestParam,因为这是一个起始页。

 @Controller
    public class ProjectController {

    @Autowired
    private ProjectService projectService;

     @RequestMapping(value={"/","/find"} , method = RequestMethod.GET)
     public String getPersonList(ModelMap model) {  

                model.addAttribute("ProjectList", projectService.getStudentProject());  

                return "find";  
     }  
 }