最近在学习spring mvc
——————————Controller代码————————————————
package test.web;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;import test.service.PersonService;@Controller
public class PersonController { @Autowired
private PersonService personService;

@RequestMapping("index")
public ModelAndView index(HttpSession session,HttpServletRequest request)
{
ModelAndView mav=new ModelAndView();//断点在这行,启动应用断点不停,点右键刷新页面断点才停,控制台均没有报错
try
{
mav.setViewName("person");
}
catch(Exception e)
{
mav.setViewName("error");
}
return mav;
}
}
—————————————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"></head>
<body>
person//我把这个换成aaa,重启应用,结果显示的还是person,右键点刷新页面,才换成aaa
</body>
</html>——————————————————————————为什么右键刷新页面才调index方法?请各位大侠看看怎么回事,哪里设置的不对吗?
还有,我想在PersonController 里定义一个变量name=“abc”,然后传到前台显示,java文件和jsp文件分别需要写什么代码?

解决方案 »

  1.   


    不调他怎么找到person.jsp的呢?
    不是通过设置mav.setViewName("person");? 
    我没有index.html,只有person.jsp和error.jsp
    下面是我的web.xml和servlet.xml
    —————————web.xml—————————————————
    <?xml version="1.0" encoding="UTF-8"?><web-app 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_3_0.xsd"
      version="3.0"
      metadata-complete="true"> <context-param> 
       <param-name>contextConfigLocation</param-name> 
       <param-value>
        /WEB-INF/applicationContext.xml,
        /WEB-INF/config/applicationContext-*.xml
        </param-value> 
    </context-param> 
        
      <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <filter>  
       <filter-name>characterEncodingFilter</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>  
       <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
       </init-param>  
      </filter>
      
      <filter-mapping>  
       <filter-name>characterEncodingFilter</filter-name>  
       <url-pattern>/*</url-pattern>  
      </filter-mapping>   
      
      <servlet>
        <servlet-name>shwtest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>shwtest</servlet-name>
        <url-pattern>*.html</url-pattern>
      </servlet-mapping>
     
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list></web-app>——————————————test-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"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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"> <mvc:annotation-driven />

    <context:component-scan base-package="test.web"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:viewClass="org.springframework.web.servlet.view.JstlView"
    p:prefix="/WEB-INF/webapp/"
    p:suffix=".jsp">
    </bean></beans>
      

  2.   


    不调他怎么找到person.jsp的呢?
    不是通过设置mav.setViewName("person");? 
    我没有index.html,只有person.jsp和error.jsp
    下面是我的web.xml和servlet.xml
    —————————web.xml—————————————————
    <?xml version="1.0" encoding="UTF-8"?><web-app 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_3_0.xsd"
      version="3.0"
      metadata-complete="true"> <context-param> 
       <param-name>contextConfigLocation</param-name> 
       <param-value>
        /WEB-INF/applicationContext.xml,
        /WEB-INF/config/applicationContext-*.xml
        </param-value> 
    </context-param> 
        
      <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <filter>  
       <filter-name>characterEncodingFilter</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>  
       <init-param>  
        <param-name>forceEncoding</param-name>  
        <param-value>true</param-value>  
       </init-param>  
      </filter>
      
      <filter-mapping>  
       <filter-name>characterEncodingFilter</filter-name>  
       <url-pattern>/*</url-pattern>  
      </filter-mapping>   
      
      <servlet>
        <servlet-name>shwtest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>shwtest</servlet-name>
        <url-pattern>*.html</url-pattern>
      </servlet-mapping>
     
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list></web-app>——————————————test-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"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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"> <mvc:annotation-driven />

    <context:component-scan base-package="test.web"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:viewClass="org.springframework.web.servlet.view.JstlView"
    p:prefix="/WEB-INF/webapp/"
    p:suffix=".jsp">
    </bean></beans>
    你服务器启动的时候,页面是不会发生改变的。
    只有页面发生请求之后,服务器作出相应,页面才会进行改变。
    另外加载和调用时两码事啊。
    加载了person.jsp并不一定等于调用啊。
      

  3.   


    可是我已经把person.jsp里显示的内容改了(把person变成aaa),重启tomcat,为什么还显示person啊?
      

  4.   


    我停了server后,把eclipse里的那个IE窗口关了啊
    然后修改的person.jsp代码
    然后点debug on server
    然后eclipse编译,控制台显示Server startup in 7542 ms
    最后,eclipse自动又在里打开一个新的IE,是嵌在eclipse内部的,显示内容还是原来的内容,刷新后才变成新改的
      

  5.   


    可是我已经把person.jsp里显示的内容改了(把person变成aaa),重启tomcat,为什么还显示person啊?
    清空浏览器缓存之后再打开看看。。
    楼主,以后描述问题准确一点。刚开始我真的理解偏了。
      

  6.   


    工具-internet选项-常规里面
    点删除清除缓存
    点设置,单选框选择“每次访问网页时”