按照老师 的操作,直接run show.jsp,页面显示 404 怎么回事呢?

按照老师 的操作,直接run show.jsp,页面显示 404 怎么回事呢?

package com.imooc.sm.controller;

import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller("testController")
public class TestController {
   public void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setAttribute("name","张三");
       request.getRequestDispatcher("show.jsp").forward(request,response);
   }
}
package com.imooc.sm.global;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DispatcherServlet extends GenericServlet {

   private ApplicationContext context;

   public void init() throws ServletException {
       super.init();
       context = new ClassPathXmlApplicationContext("spring.xml");
   }

   public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
       HttpServletRequest request = (HttpServletRequest) servletRequest;
       HttpServletResponse response = (HttpServletResponse) servletResponse;

       /*
              staff/add.do        login.do
              staffController
              public void add(HttpServletRequest request, HttpServletResponse response){}
       *
        */
       String path = request.getServletPath().substring(1);
       String beanName = null;
       String methodName = null;
       int index = path.indexOf('/');
       if (index != -1) {
           beanName = path.substring(0, index) + "Controller";
           methodName = path.substring(index + 1, path.indexOf(".do"));
       } else {
           beanName = "selfController";
           methodName = path.substring(0, path.indexOf(".do"));
       }

       Object obj = context.getBean(beanName);
       try {
           Method method = obj.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
           method.invoke(obj,request,response);
       } catch (NoSuchMethodException e) {
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       } catch (InvocationTargetException e) {
           e.printStackTrace();
       }
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee">


<filter>
 <filter-name>Encoding</filter-name>
 <filter-class>com.imooc.sm.global.EncodingFilter</filter-class>
 <init-param>
   <param-name>ENCODING</param-name>
   <param-value>UTF-8</param-value>
 </init-param>
</filter>
 <filter-mapping>
   <filter-name>Encoding</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <servlet>
   <servlet-name>Global</servlet-name>
   <servlet-class>com.imooc.sm.global.DispatcherServlet</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>Global</servlet-name>
   <url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app>

<?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: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/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd">


   <!--Spring 整合 mybatis-->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/sm?useUnicode=true&amp;characterEncoding=UTF-8"/>
       <property name="username" value="root"/>
       <property name="password" value="root"/>
    </bean>
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <property name="typeAliasesPackage" value="com.imooc.sm.entity"/>
   </bean>
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.imooc.sm.dao"/>
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
   </bean>

   <!--声明式事务-->
   <!--事务管理器-->
   <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
   </bean>
   <!--通知-->
   <tx:advice id="txAdvice" transaction-manager="transcationManager">
       <tx:attributes>
           <tx:method name="get*" read-only="true"/>
           <tx:method name="find*" read-only="true"/>
           <tx:method name="search*" read-only="true"/>
           <tx:method name="*" propagation="REQUIRED"/>
       </tx:attributes>
   </tx:advice>
   <!--植入-->
   <aop:config>
       <aop:pointcut id="txPointcut" expression="execution(* com.imooc.sm.service.*.*(..))"/>
       <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
   </aop:config>
   <!--全局扫描-->
   <context:component-scan base-package="com.imooc.sm"/>
   <aop:aspectj-autoproxy/>
</beans>

http://img1.sycdn.imooc.com//climg/5ebf686f094b566b02670263.jpg

正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学你好,问一下同学说的name值没有传到网页,页面中是显示空白吗?还是显示${NAME}?如果显示是${NAME},是因为该jsp不支持el表达式,建议同学在头部增加如下代码再试试:

<%@page isELIgnored="false" %>

如果不是上边所说的情况,建议同学将具体问题贴一下。

祝:学习愉快~

提问者 慕仰0394571 2020-05-16 19:48:51

刚才试了一下,可以跳转到 show.jsp 了,可是name的值没传到网页

好帮手慕小班 2020-05-16 17:11:34

同学你好,测试同学代码是可以正常访问的,比如:

http://img1.sycdn.imooc.com//climg/5ebfadc0093cae9b09520349.jpg

http://img1.sycdn.imooc.com//climg/5ebfade2097d379004400095.jpg

如上所示,同学可以检查一下自己的路径,是否需要书写项目名

http://img1.sycdn.imooc.com//climg/5ebfae280934ef2407820287.jpg

比如这里配置了项目名,对应在地址中也要书写项目名。

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 慕仰0394571 #1
    刚才试了一下,可以跳转到 show.jsp 了,可是name的值没传到网页,是哪里出问题了?
    2020-05-16 19:49:49
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师