当前位置:编程学习 > JAVA >>

新人spring mvc 404错误,请求帮助

web.xml

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


<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>/</url-pattern>
</servlet-mapping>


</web-app>


spring-servet.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">

<!-- 自动扫描bean,把作了注解的类转换为bean -->
<context:component-scan base-package="com.mvc.rest" />

<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>


RestController
package com.mvc.rest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RestController {

    public RestController(){

    }

    @RequestMapping(value = "/login/{user}", method = RequestMethod.GET)
    public ModelAndView myMethod(HttpServletRequest request, HttpServletResponse response,
            @PathVariable("user") String user, ModelMap modelMap) throws Exception {
        modelMap.put("loginUser", user);
        return new ModelAndView("/login/hello", modelMap);
    }

     @RequestMapping(value = "/welcome", method = RequestMethod.GET)
        public String registPost() {
         return "/welcome";
        }
}

--------------------编程问答-------------------- 我也没看出来,我发我一般做的路径你,你看看
-----------------------------spring-config.xml---------------------------------------
<?xml version="1.0" encoding="UTF-8"?>

<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName">

<!-- 控制上传文件的大小 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600"/>
        <property name="maxInMemorySize" value="4096"/>
   </bean>

<!-- 把此包下的所有标示注解的类注入 -->
<context:component-scan base-package="com"></context:component-scan>

<import resource="spring-ibatis.xml" />
<import resource="spring-mvc.xml" />

<import resource="emptyForSpringParse_beans.xml" />

<import resource="spring-model-auth.xml" />
<import resource="spring-model-system.xml" />

<import resource="spring-wwt.xml" />
<import resource="spring-zyc.xml" />
<import resource="spring-wzl.xml" />
<import resource="spring-jgx.xml" />
<import resource="spring-sf.xml" />
<import resource="spring-wfl.xml" />
<import resource="spring-wp.xml" />
<import resource="spring-ljc.xml" />
<import resource="spring-lkk.xml" />
    <import resource="spring-ybb.xml" />
    <import resource="spring-xj.xml" />
</beans>

-------------------------spring-ljc.xml----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>

<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName">

<bean id="formDaoImpl" class="com.freeplatform.system.dao.impl.FormDaoImpl">
     <property name="dataSource">
     <ref bean="dataSource"/>
     </property>
    </bean>
    
     <bean name="formServiceImpl" class="com.freeplatform.system.service.impl.FormServiceImpl"> 
        <property name="formDaoImpl"> 
            <ref bean="formDaoImpl" /> 
        </property> 
    </bean>

<bean name="/system/FormTest/toList.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/queryItem.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/queryHuiti.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/queryList.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/toExcel.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/toPrint.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/toContractTable.do" class="com.freeplatform.system.controller.FormTestController" />

</beans>
--------------------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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>energy</display-name>
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/profile/spring/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>GB2312</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <description></description>
    <display-name>System</display-name>
    <servlet-name>System</servlet-name>
    <servlet-class>com.freeplatform.framework.singleton.ParameterServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>System</servlet-name>
    <url-pattern>/System</url-pattern>
  </servlet-mapping>
  <jsp-config>
    <taglib>
      <taglib-uri>/free</taglib-uri>
      <taglib-location>/WEB-INF/classes/com/freeplatform/framework/tag/free_tag.tld</taglib-location>
    </taglib>
  </jsp-config>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/profile/log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>EncodeServlet</display-name>
    <servlet-name>EncodeServlet</servlet-name>
    <servlet-class>com.freeplatform.workflow.controller.EncodeServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>EncodeServlet</servlet-name>
    <url-pattern>/EncodeServlet</url-pattern>
  </servlet-mapping>
</web-app>

---------------controller-------------------------
package com.freeplatform.energy.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.web.servlet.ModelAndView;

import com.freeplatform.auth.service.AuthParameterService;
import com.freeplatform.energy.bean.DianbiaoBeanExtends;
import com.freeplatform.energy.service.DianbiaoService;
import com.freeplatform.framework.commons.BaseController;
import com.freeplatform.framework.singleton.DictParameter;
import com.freeplatform.report.bean.DCurvedataBeanExtends;
import com.freeplatform.report.service.DCurvedataService;
import com.freeplatform.system.bean.SysDictItemBeanExtends;

/**
 * 
 * @author CodeBuilder V1.0
 * @version 1.0
 */
public class DianwangController extends BaseController {

private DianbiaoService dianbiaoServiceImpl;
private DCurvedataService dCurvedataServiceImplReport;
// private boolean falg = false;
private AuthParameterService authParameterServiceImpl;

public DianbiaoService getDianbiaoServiceImpl() {
return dianbiaoServiceImpl;
}

public void setDianbiaoServiceImpl(DianbiaoService dianbiaoServiceImpl) {
this.dianbiaoServiceImpl = dianbiaoServiceImpl;
}

public DCurvedataService getdCurvedataServiceImplReport() {
return dCurvedataServiceImplReport;
}

public void setdCurvedataServiceImplReport(
DCurvedataService dCurvedataServiceImplReport) {
this.dCurvedataServiceImplReport = dCurvedataServiceImplReport;
}

public AuthParameterService getAuthParameterServiceImpl() {
return authParameterServiceImpl;
}

public void setAuthParameterServiceImpl(
AuthParameterService authParameterServiceImpl) {
this.authParameterServiceImpl = authParameterServiceImpl;
}

public DianwangController() {

}

/**
 * 列表页面
 * 
 * @param request
 * @param response
 * @param bean
 * @return
 * 
 */
public ModelAndView toListPage(HttpServletRequest request,
HttpServletResponse response, DianbiaoBeanExtends bean) {
Map<String, Object> model = new HashMap<String, Object>();

model.put("dianwang", bean);

return new ModelAndView("/energy/dianwang/dianwang_list", model);
}


仔细看看,这玩意考的就是细心,希望对你又所帮助 --------------------编程问答-------------------- 我也没看出来,我发我一般做的路径你,你看看
-----------------------------spring-config.xml---------------------------------------
<?xml version="1.0" encoding="UTF-8"?>

<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName">

<!-- 控制上传文件的大小 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600"/>
        <property name="maxInMemorySize" value="4096"/>
   </bean>

<!-- 把此包下的所有标示注解的类注入 -->
<context:component-scan base-package="com"></context:component-scan>

<import resource="spring-ibatis.xml" />
<import resource="spring-mvc.xml" />

<import resource="emptyForSpringParse_beans.xml" />

<import resource="spring-model-auth.xml" />
<import resource="spring-model-system.xml" />

<import resource="spring-wwt.xml" />
<import resource="spring-zyc.xml" />
<import resource="spring-wzl.xml" />
<import resource="spring-jgx.xml" />
<import resource="spring-sf.xml" />
<import resource="spring-wfl.xml" />
<import resource="spring-wp.xml" />
<import resource="spring-ljc.xml" />
<import resource="spring-lkk.xml" />
    <import resource="spring-ybb.xml" />
    <import resource="spring-xj.xml" />
</beans>

-------------------------spring-ljc.xml----------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>

<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName">

<bean id="formDaoImpl" class="com.freeplatform.system.dao.impl.FormDaoImpl">
     <property name="dataSource">
     <ref bean="dataSource"/>
     </property>
    </bean>
    
     <bean name="formServiceImpl" class="com.freeplatform.system.service.impl.FormServiceImpl"> 
        <property name="formDaoImpl"> 
            <ref bean="formDaoImpl" /> 
        </property> 
    </bean>

<bean name="/system/FormTest/toList.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/queryItem.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/queryHuiti.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/queryList.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/toExcel.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/toPrint.do" class="com.freeplatform.system.controller.FormTestController" />
<bean name="/system/FormTest/toContractTable.do" class="com.freeplatform.system.controller.FormTestController" />

</beans>
--------------------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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>energy</display-name>
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/profile/spring/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>GB2312</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <description></description>
    <display-name>System</display-name>
    <servlet-name>System</servlet-name>
    <servlet-class>com.freeplatform.framework.singleton.ParameterServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>System</servlet-name>
    <url-pattern>/System</url-pattern>
  </servlet-mapping>
  <jsp-config>
    <taglib>
      <taglib-uri>/free</taglib-uri>
      <taglib-location>/WEB-INF/classes/com/freeplatform/framework/tag/free_tag.tld</taglib-location>
    </taglib>
  </jsp-config>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/profile/log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>EncodeServlet</display-name>
    <servlet-name>EncodeServlet</servlet-name>
    <servlet-class>com.freeplatform.workflow.controller.EncodeServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>EncodeServlet</servlet-name>
    <url-pattern>/EncodeServlet</url-pattern>
  </servlet-mapping>
</web-app>

---------------controller-------------------------
package com.freeplatform.energy.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.web.servlet.ModelAndView;

import com.freeplatform.auth.service.AuthParameterService;
import com.freeplatform.energy.bean.DianbiaoBeanExtends;
import com.freeplatform.energy.service.DianbiaoService;
import com.freeplatform.framework.commons.BaseController;
import com.freeplatform.framework.singleton.DictParameter;
import com.freeplatform.report.bean.DCurvedataBeanExtends;
import com.freeplatform.report.service.DCurvedataService;
import com.freeplatform.system.bean.SysDictItemBeanExtends;

/**
 * 
 * @author CodeBuilder V1.0
 * @version 1.0
 */
public class DianwangController extends BaseController {

private DianbiaoService dianbiaoServiceImpl;
private DCurvedataService dCurvedataServiceImplReport;
// private boolean falg = false;
private AuthParameterService authParameterServiceImpl;

public DianbiaoService getDianbiaoServiceImpl() {
return dianbiaoServiceImpl;
}

public void setDianbiaoServiceImpl(DianbiaoService dianbiaoServiceImpl) {
this.dianbiaoServiceImpl = dianbiaoServiceImpl;
}

public DCurvedataService getdCurvedataServiceImplReport() {
return dCurvedataServiceImplReport;
}

public void setdCurvedataServiceImplReport(
DCurvedataService dCurvedataServiceImplReport) {
this.dCurvedataServiceImplReport = dCurvedataServiceImplReport;
}

public AuthParameterService getAuthParameterServiceImpl() {
return authParameterServiceImpl;
}

public void setAuthParameterServiceImpl(
AuthParameterService authParameterServiceImpl) {
this.authParameterServiceImpl = authParameterServiceImpl;
}

public DianwangController() {

}

/**
 * 列表页面
 * 
 * @param request
 * @param response
 * @param bean
 * @return
 * 
 */
public ModelAndView toListPage(HttpServletRequest request,
HttpServletResponse response, DianbiaoBeanExtends bean) {
Map<String, Object> model = new HashMap<String, Object>();

model.put("dianwang", bean);

return new ModelAndView("/energy/dianwang/dianwang_list", model);
}


仔细看看,这玩意考的就是细心,希望对你又所帮助 --------------------编程问答-------------------- --------------------编程问答-------------------- spring-servet.xml要放在web-inf下面,要不就要修改web.xml下面的路径,不知道楼主你说的404具体是什么 --------------------编程问答-------------------- LZ把控制台输出的异常信息贴出来,那样有利于问题的解答
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,