我在看《Spring技术手册》,跟着上面的例子做,可是第一个SpringMVC的例子就没运行出来,现在把代码贴出来,麻烦帮我看下吧,谢谢了!!!web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" >
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>

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

</web-app>
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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

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

<bean name="/hello.do" class="controller.HelloController">
<property name="viewPage" value="hello" />
</bean>

</beans>HelloController.java
package controller;import javax.servlet.http.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;public class HelloController implements Controller{
    private String viewPage;
    
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception{
     String user = req.getParameter("user");
     return new ModelAndView(viewPage,"user",user);
    }
public void setViewPage(String viewPage) {
this.viewPage = viewPage;
}
     
}hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>First Spring MVC</title>
</head>
<body>
  <h1>Hello, ${user}!!</h1>
</body>
</html>运行后,在地址栏里输入的是:http://localhost:8080/FirstSpringMVC/hello.do?user=Justin运行后显示错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/hello.do' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'viewPage' of bean class [controller.HelloController]: No property 'viewPage' found
我研究了好半天了,没觉得有什么错啊?刚开始以为HelloController.java里面要加getViewPage,可是试过了以后也还是这个错误,这到底是怎么回事啊?谢谢大家了!!!

解决方案 »

  1.   

    <bean name="/hello.do" class="controller.HelloController">建议去掉斜杠试试 
      

  2.   

    public class HelloController implements Controller{
      private String viewPage;
       
      public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception{
      String user = req.getParameter("user");
      return new ModelAndView(viewPage,"user",user);
      }
    public void setViewPage(String viewPage) {
    this.viewPage = viewPage;
    }
       
    }
    get 方法加上
      

  3.   

    这两个方法我早都试过了,get方法加过了,斜杠也去掉过了,都不行
      

  4.   


    哥啊 http://localhost:8080/FirstSpringMVC/hello.do?user=Justin 你这个是说明意思?这里的hello.do是个什么?我从你的代码里感觉他是一个java类 你以为你写的是一个action吗?你别光顾着敲例子了  你先理解流程吧···
      

  5.   

    自选 spring的娃娃 伤不起 哈哈 我明白了 你有java基础吗
      

  6.   

    我在网上找了下,有这样一段话:基于Spring的Web应用程序接收到http://localhost:8080/hello.do(事实上请求路径是/hello.do)的请求后,Spring将这个请求交给一个名为helloController的程序进行处理,helloController再调用一个名为hello.jsp的jsp文件生成HTML代码发给用户的浏览器显示。上面的名称(/hello.do,helloController,hello.jsp)都是变量,你可以更改。这个hello.do既不是java类,也不是action吧?只是为dispatcherServlet指明了要去找哪个controller我不知道自己的理解对不对
      

  7.   

    http://localhost:8080/FirstSpringMVC/hello.do?user=Justin
    http://localhost:8080/hello.do
    两个一个有项目名字 一个没有项目名字 搞不清楚状况啊···你以上理解正确·
      

  8.   

    有项目的话 它先找到项目 再找hello.do
    没项目的话 你访问hello.do必须404或者500 不信试试 我忙着 不和你说了
      

  9.   

    使用spring3的注解吧,好简单
    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">


        <context-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/springmvc-servlet.xml</param-value>  
        </context-param>  
        <listener>  
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
        </listener>  
     
          
          
        <!--Character Encoding Convert-->  
        <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>  
            <url-pattern>*.do</url-pattern>  
        </filter-mapping>  
        <!-- Spring Security Filter-->  
       <filter>

              
        <servlet>  
            <servlet-name>springmvc</servlet-name>  
            <servlet-class> org.springframework.web.servlet.DispatcherServlet   
            </servlet-class>   
        </servlet>  
        <servlet-mapping>  
            <servlet-name>springmvc</servlet-name>  
            <url-pattern>*.do</url-pattern>  
        </servlet-mapping>  
          
     

      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>springmv-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:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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/tx  
                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
                http://www.springframework.org/schema/jdbc  
                http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    <!-- 自动搜索@Controller标注的类 -->
    <context:component-scan base-package="com.zuxiang.web" /> <!-- Default ViewResolver -->
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
    value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/" />
    <property name="suffix" value=".jsp"></property>
    </bean>
    <bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource"
    p:basename="i18n/messages" />
    </beans>
    action  package com.zuxiang.web;import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;@Controller
    @RequestMapping("/user")//可以不要,那请求的路径就是“/”
    public class UserAction {
    //也可以用实体类
    /* 
    @RequestMapping("/login")
    public String preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
    User user) throws Exception {
    // TODO Auto-generated method stub
       if("zx".equals(user.getName)&&"zz".equals(user.getPassword)){
       System.out.println("ok");
       return "ok";
       }else{
       return "error";
       }
     


    }
     */
    @RequestMapping("/login")
    public String preHandle(HttpServletRequest arg0, HttpServletResponse arg1,
    String name ,String password) throws Exception {
    // TODO Auto-generated method stub
       if("zx".equals(name)&&"zz".equals(password)){
       System.out.println("ok");
       return "ok";
       }else{
       return "error";
       }
     


    }}login.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'admin.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>  
     
                <div id="content">  
         
                    <h2>SpringMVCDemo示例</h2>  
                    <h3>--CRUD管理界面演示</h3>  
                    <form id="loginForm" action="user/login.do" method="post">  
                        <table class="inputView">  
                            <tr>  
                                <td>  
                                    用户名:  
                                </td>  
                                <td>  
                                    <input type='text' name='name'  
      
                                </td>  
                            </tr>  
                            <tr>  
                                <td>  
                                    密码:  
                                </td>  
                                <td>  
                                    <input type='password' name='password' class="required" />  
                                </td>  
                            </tr>  
                            <tr>  
                                <td colspan='2'>  
                                  
                                      <input value="登录" type="submit" />  
                                </td>  
                            </tr>  
                        </table>  
                    </form>  
                </div>  
            </body>  
        </html>  
      

  10.   

    参考14L,看看基于注解的SpringMVC,从spring2.5开始springMVC都支持注解,不知道你用的spring版本是多少,添加注解看看,
      

  11.   

    bean 里面写的是id就是自己定的名字,你不要定/...do啊,.do
    一般是写struts 里面的action
      

  12.   

    你工程里面有没有/WEB-INF/dispatcher-servlet.xml这个配置文件,你把它注入到web.xml里面试试
    应该是bean没有注入的问题。没环境不然可以帮你试试,慢慢来吧~
      

  13.   


    我有/WEB-INF/dispatcher-servlet.xml这个配置文件,但是不知怎么注入到web.xml里面啊,能教教我吗?谢谢了!!!!