类级别的@RequestParam注释未按预期工作

时间:2017-01-27 14:11:52

标签: spring spring-mvc

这是控制器:

        package com.spring.controller;

        import java.util.Arrays;
        import java.util.List;

        import javax.servlet.http.HttpSession;

        import org.jboss.logging.annotations.Param;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.ModelAttribute;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.servlet.ModelAndView;

        import com.spring.DAO.CountryDAO;
        import com.spring.DAO.HospitalDAO;
        import com.spring.DAO.HospitalSpecialityDAO;
        import com.spring.DAO.SpecialityDAO;
        import com.spring.VO.HospitalSpecialityVO;
        import com.spring.VO.HospitalVO;
        import com.spring.VO.SpecialityVO;

        @Controller
        @RequestMapping("/admin")
        public class HospitalController {

            @Autowired
            CountryDAO country;
            @Autowired
            HospitalDAO hospital;
            @Autowired
            SpecialityDAO Speciality;
            @Autowired
            HospitalSpecialityDAO hospitalSpeciality;

    @RequestMapping("/addHospital.html")
    public ModelAndView addHospital(HttpSession session) throws Exception {
      List<Object> list = this.country.getCountry();
      session.setAttribute("list", list);
      List<Object> slist = this.Speciality.getSpeciality();
      session.setAttribute("slist", slist);
      return new ModelAndView("admin/addHospital",
            "insertHospitalSpeciality", new HospitalSpecialityVO());

    }
   }

这是我要映射的网址:

/projectname/admin/addHospital.html

这是web.xml文件:

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

                <servlet>
                    <servlet-name>spring</servlet-name>

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

                <servlet-mapping>
                    <servlet-name>spring</servlet-name>
                    <url-pattern>*.html</url-pattern>
                </servlet-mapping>

            <listener>
            <listener-class>
            org.springframework.web.context.ContextLoaderListener
            </listener-class>
            </listener>
            <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-security.xml</param-value>
            </context-param>
            <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
            </filter-class>
            </filter>
            <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
            </filter-mapping>


            </web-app>

spring-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:p="http://www.springframework.org/schema/p"  
                xmlns:context="http://www.springframework.org/schema/context"  
                xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

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



                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
                    <property name="prefix" value="WEB-INF/view/" />  
                    <property name="suffix" value=".jsp" />  
                </bean>  
                <bean id="multipartResolver"
                    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

                     <!-- setting maximum upload size -->
                    <property name="maxUploadSize" value="100000" />

                </bean>
                <!-- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
                    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost/healthanalytics"/>
                    <property name="username" value="root"/>
                    <property name="password" value="root"/>
                </bean> -->

                <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                    <property name="dataSource" ref="dataSource" />

                     <property name="annotatedClasses">
                        <list>
                            <value>com.spring.VO.CityVO</value>
                            <value>com.spring.VO.ComplainVO</value>
                            <value>com.spring.VO.CountryVO</value>
                            <value>com.spring.VO.DiseaseVO</value>
                            <value>com.spring.VO.DoctorVO</value>
                            <value>com.spring.VO.FeedbackVO</value>
                            <value>com.spring.VO.HospitalVO</value>
                            <value>com.spring.VO.MedicineVO</value>
                            <value>com.spring.VO.PatientVO</value>
                            <value>com.spring.VO.StateVO</value>
                            <value>com.spring.VO.SpecialityVO</value>
                            <value>com.spring.VO.SymptomVO</value>
                            <value>com.spring.VO.HospitalSpecialityVO</value>
                            <value>com.spring.VO.DoctorSpecialityVO</value>
                            <value>com.spring.VO.DoctorHospitalVO</value>
                            <value>com.spring.VO.RegistrationVO</value>
                            <value>com.spring.VO.LoginVO</value>




                            <!-- <value>com.malhar.model.Login</value> -->
                        </list>
                    </property>
                    <property name="hibernateProperties">
                        <props>
                            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                            <prop key="hibernate.show_sql">true</prop>
                            <prop key="hibernate.hbm2ddl.auto">update</prop>
                            <prop key="hibernate.enable_lazy_load_load_trans">true</prop>
                        </props>
                    </property>
                </bean>      

            </beans>

但是当我打电话给以下网址时:

http://localhost:8080/health/admin/addHospital.html

我明白了:

  

HTTP状态404 - /health/admin/WEB-INF/view/admin/addHospital.jsp

此处health是项目名称。

我无法弄清楚,在获得结果时,为什么它会尝试将admin放在WEB-INF之前,如错误所示。

为什么它只能从/health/WEB-INF/view/admin/addHospital.jsp得到结果?

此外,如果任何人都可以为此提供任何替代解决方案,那将会非常有用。

1 个答案:

答案 0 :(得分:1)

在您的视图解析程序配置中,将/放在WEB-INF/view/之前,对于CSS和JavaScript资源,使用mvc:resources标记来配置资源位置,并在JSP中使用c:url导入它们。