springMVC多个controller总是爆出404

呜呜呜呜喂喂喂 发布于 2015/07/15 09:28
阅读 2K+
收藏 0
JAVA程序:
package com.dhc.web.controller;


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


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;


public class MultiController extends MultiActionController{


public ModelAndView add(HttpServletRequest request,HttpServletResponse response){
System.out.println("----add----");
return new ModelAndView("/welcome","method","add");

}
public ModelAndView update(HttpServletRequest request,HttpServletResponse response){
System.out.println("----update----");
return new ModelAndView("/welcome","method","update");

}


}




springMVC-servlet.xml:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 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:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        ">
    <bean name="/test1/multi" class="com.dhc.web.controller.MultiController">
      <property name="methodNameResolver">
      <ref bean="paramNameResolver"/>
      </property>
    </bean>
    <bean id="paramNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"></property>
</bean>      

    <bean name="/test1/helloworld" class="com.dhc.web.controller.HelloWorldController" ></bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>


</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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springMVCtext1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:config/springMVC-servlet.xml/</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div><h1>多方法controller</h1>
本次方法位${method}</div>
</body>
</html>

启动tomcat提后在浏览器中输入http://localhost:8080/springMVCtext1/test1/multi?action=add

页面显示404,求大神帮忙解答

项目结构:
加载中
0
beidao-ai
beidao-ai
 <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>

  </servlet-mapping>

将这个改成:

 <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/*</url-pattern>

  </servlet-mapping>

然后在springMVC-servlet.xml中加入

<mvc:resources mapping="/images/**" location="/images/" /> 

等过滤静态资源


0
泥瓦匠BYSocket
泥瓦匠BYSocket
用注解 一目了然

http://spring.io/guides/gs/serving-web-content/

返回顶部
顶部