2 qq 36817949 qq_36817949 于 2017.01.12 14:13 提问

springMVC无法访问controller,这是怎么回事啊

spring配置

 <?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- 开启注解扫描 -->
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <!-- 扫描controller和service --> 
    <context:component-scan base-package="com.jinJi" use-default-filters="true"></context:component-scan> 

     <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
     <!-- 以字节为单位的最大上传文件的大小 --> 
     <property name="maxUploadSize" value="100000" /> 
     </bean> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/jsp/"></property> 
        <property name="suffix" value=".jsp"></property> 
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
    </bean> 


</beans>


web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    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">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>  
       <param-name>contextConfigLocation</param-name>  
       <param-value>classpath:applicationContext.xml</param-value>  
    </context-param> 
    <!-- 默认欢迎界面 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <!-- 网站编码设置为UTF-8 -->
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <servlet-name>springmvc</servlet-name>
    </filter-mapping>

    <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:applicationContext.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>


</web-app>

Controller

 package com.jinji.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jinji.pojo.KeHu;
import com.jinji.pojo.LiuYan;
import com.jinji.pojo.YongHu;
import com.jinji.pojo.ZHAOPIN;
import com.jinji.service.JinJiService;


@Controller
@RequestMapping("/jinji")
public class JinJiController {

    @Resource(name="jinJiService")
    JinJiService jinjiService ;

    @ResponseBody
    @RequestMapping("wangzhan1")
    public String wangzhan1(Map request){
        System.out.println("wangzhan1");
        return "wangzhan1";
    }

    @RequestMapping("Liu")
    public String Liu(LiuYan liuyan,Map request){
        jinjiService.saveLIUYAN(liuyan);
        return "redirect:/jinji/liuyan2.do";
    }

    @RequestMapping("card1")
    public String card1(Map request){

        return "card1";
    }
    @RequestMapping("jianjie1")
    public String jianjie1(Map request){
        List<Map> m=jinjiService.queryGong();
        System.out.println(m.size());
        request.put("map", jinjiService.queryGong());

        return "jianjie1";
    }

    @RequestMapping("case1")
    public String case1(Map request){
        request.put("list", jinjiService.querykehu());
        return "case";
    }

    @RequestMapping("lianxi")
    public String lianxi(Map request){

        return "lianxi";
    }
    @RequestMapping("zhaopin")
    public String zhaopin(Map request){

         request.put("list", jinjiService.queryZHAOPIN());

        return "zhaopin";
    }
    @RequestMapping("liuyan2")
    public String lianyan2(Map request){

        request.put("RIQI", jinjiService.queryRIQI());


        return "liuyan2";

    }
    @RequestMapping("copyright1")
    public String copyright1(Map request){

        return "copyright1";
    }
    @RequestMapping("card2")
    public String card2(Map request){

        return "card2";
    }
    @RequestMapping("card3")
    public String card3(Map request){

        return "card3";
    }
    @RequestMapping("card4")
    public String card4(Map request){

        return "card4";
    }
    @RequestMapping("card5")
    public String card5(Map request){

        return "card5";
    }
    @RequestMapping("card6")
    public String card6(Map request){

        return "card6";
    }
    @RequestMapping("card7")
    public String card7(Map request){

        return "card7";
    }
    @RequestMapping("card8")
    public String card8(Map request){

        return "card8";
    }
    @RequestMapping("card9")
    public String card9(Map request){

        return "card9";
    }
    @RequestMapping("card10")
    public String card10(Map request){

        return "card10";
    }
    @RequestMapping("card11")
    public String card11(Map request){

        return "card11";
    }
    @RequestMapping("card12")
    public String card12(Map request){

        return "card12";
    }
    @RequestMapping("card13")
    public String card13(Map request){

        return "card13";
    }

    @RequestMapping("card14")
    public String card14(Map request){

        return "card14";
    }

    @RequestMapping("delu")
    public String delu(Map request){

        return "delu/delu";
    }

    @RequestMapping("guanli")
    public String guanli(Map request){

        return "houtai/guanli";
    }

    @RequestMapping("show")
    public String show(Map request){

        request.put("list", jinjiService.queryguanli());
        return "houtai/show";
    }

    @RequestMapping("nav")
    public String nav(Map request){
        request.put("list", jinjiService.queryZHAOPIN());
        return "houtai/nav";
    }

    @RequestMapping("page")
    public String page(Map request){
        request.put("list", jinjiService.queryliuyan());
        return "houtai/page";
    }

    @RequestMapping("system")
    public String system(Map request){
        request.put("list", jinjiService.querykehu());
        return "houtai/system";
    }

    @RequestMapping("gong")
    public String gong(Map request){

        request.put("list", jinjiService.queryGong());

        return "houtai/gong";
    }

    @RequestMapping("deleteGUANLI")
    public String deleteGUANLI(String did,Map request){

        jinjiService.deleteGUANLI(did);
        return "redirect:/jinji/show.do";
    }

    @RequestMapping("deleteKehu")
    public String deleteKehu(String kid,Map request){

        jinjiService.deleteKeHu(kid);
        return "redirect:/jinji/system.do";
    }

    @RequestMapping("deleteGong")
    public String deleteGong(String gid,Map request){

        jinjiService.deleteGong(gid);
        return "redirect:/jinji/gong.do";
    }
    @RequestMapping("deletZHAOPIN")
    public String deleteZHAOPIN(String zid,Map request){

        jinjiService.deleteZhaoPin(zid);
        return "redirect:/jinji/nav.do";
    }

    @RequestMapping("deletLiuYan")
    public String deleteLiuYan(String lid,Map request){

        jinjiService.deleteLiuYan(lid);
        return "redirect:/jinji/page.do";
    }

    @RequestMapping("saveGUANLI")
    public String saveGUANLI(YongHu yonghu,Map request){

        jinjiService.savaGuanLi(yonghu);
        return "redirect:/jinji/show.do";
    }

    @RequestMapping("saveKeHu")
    public String saveKeHu(KeHu kehu,Map request){

        jinjiService.savaKeHu(kehu);
        return "redirect:/jinji/system.do";
    }

    @RequestMapping("saveZHAOPIN")
    public String saveZHAOPIN(ZHAOPIN zhaopin,Map request){

        jinjiService.savaZHAOPIN(zhaopin);
        return "redirect:/jinji/nav.do";
    }
    @RequestMapping("saveGong")
    public String saveGong(String gong,Map request){

        jinjiService.saveGong(gong);
        return "redirect:/jinji/gong.do";
    }

    @RequestMapping(value = "denglu", method = RequestMethod.POST) 

    public String denglu(String USERNAME,String PASSWORD,Map request){

        List<Map> list = jinjiService.denglu(USERNAME,PASSWORD);



    if(list.size()==1){

            return "redirect:/jinji/guanli.do";

        }else{

            return "delu/delu";
        }
    }



}

11个回答

csxiaoz
csxiaoz   2017.01.12 14:24
已采纳
  1. springmvc*.do

*.do改为/
2.请确认你的访问路径正确
/jinji/wangzhan1
3.确认 wangzhan1.jsp在/WEB-INF/jsp/下

qq_36817949
qq_36817949 不行呀
11 个月之前 回复
ZHP_CHANGE
ZHP_CHANGE   2017.01.12 14:16

*.do 把这个改成 /
然后直接访问/jinji试试

qq_36817949
qq_36817949 还是404,
11 个月之前 回复
ZHP_CHANGE
ZHP_CHANGE   2017.01.12 14:22

@RequestMapping("Liu")
public String Liu(LiuYan liuyan,Map request){
jinjiService.saveLIUYAN(liuyan);
return "redirect:/jinji/liuyan2.do";
}
修改映射 @RequestMapping("/Liu"),访问/jinji/Liu

qq_36817949
qq_36817949 不行的
11 个月之前 回复
csxiaoz
csxiaoz   2017.01.12 14:41

给你的项目文件截图发下。
还有你访问的url。

CodingBlackHole
CodingBlackHole   2017.01.12 15:14

该答案请自行跳过

H1H1H1H1H1

H2H2H2H2H2

BBBBBBBBBBB
IIIIIIIIIIIIIIIIIIIIIIIIIIII

123213123

  • * * * * * 123123123123

  • 123123123
  • 12312321312
  • 123213123
  1. 123213213
  2. 12312312312
  3. 1233123213123

www.baidu.com

qq_37146225
qq_37146225   2017.01.12 15:42

@Controller
@RequestMapping("/jinji")
public class JinJiController {

@Resource(name="jinJiService")
JinJiService jinjiService ;

@ResponseBody
@RequestMapping("wangzhan1")
public String wangzhan1(Map request){
    System.out.println("wangzhan1");
    return "wangzhan1";
}


    把类上面的@RequestMapping("/jinji")改成@RequestMapping("jinji")去掉斜杠,然后页面访问地址为jinji/wangzhan1
ma951021
ma951021   2017.01.12 15:39

重定向需要加.do吗? 好像不需要把

u012754523
u012754523   2017.01.12 15:55

1、*.do改为/*.do试一下

2、上面springmvc的配置是写在了application.xml中吗?图片这个位置写的文件名对吗?
图片说明

sinat_33388558
sinat_33388558   2017.01.12 17:11


应该不用再配了,去掉试试

sinat_33388558
sinat_33388558   2017.01.12 17:16

listener 应该不用在配了 已经配了DispatcherServlet

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!