ssm之路(12)springmvc-前端控制器配置+idea中正确的配置jstl语法和使用EL表达式

首先添加jstl和standard的jar包依赖,使得jsp中支持jstl语法

   <!-- 添加jstl依赖,使其支持<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>jstl语法 -->
    <!-- jstl标签库相关 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-jstlel</artifactId>
      <version>1.2.5</version>
    </dependency>

接着在itemsList.jsp文件中引入标签:(idea中开发,其jsp页面还得加上<%@page isELIgnored="false" %>标签,使其jsp支持EL表达式,否则EL表达式会失效

使用IDEA编写jsp时EL表达式不起作用的问题及解决方法

解决方法有两种:

1.在JSP开头添加

<%@page isELIgnored="false"%>

isELIgnored是指是否忽略EL表达式

isELIgnored 属性JSP 2.0 新引入的属性,默认值为 true

2,或者在web.xml中整体的开启或者关闭EL表达式:(注意<web-app ......>后面要有xlmn,xsi等配置,否则,直接配置<jsp-config>会报错)
 

<?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_2_5.xsd"
         id="WebApp_ID" version="2.5">
<jsp-config>

<jsp-property-group>

<url-pattern>*.jsp</url-pattern>

<el-ignored>false</el-ignore>

</jsp-property-group>

</jsp-config>

</webapp>

下面补充一下,web.xml中全局配置jsp的用法:

<?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_2_5.xsd"
         id="WebApp_ID" version="2.5">

<display-name>Archetype Created Web Application</display-name>
 <!-- 设置会话过期时间
    <session-config>
        <session-timeout>180</session-timeout>
    </session-config>

    &lt;!&ndash;允许或者禁止EL语言&ndash;&gt;
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*</url-pattern>
            <el-ignored>false</el-ignored>
        </jsp-property-group>

        &lt;!&ndash;允许或者禁止脚本段&ndash;&gt;
        <jsp-property-group>
            <url-pattern>*</url-pattern>
            <scripting-invalid>false</scripting-invalid>
        </jsp-property-group>

        &lt;!&ndash; 申明JSP的编码&ndash;&gt;
        <jsp-property-group>
            <url-pattern>*</url-pattern>
            <page-encoding>GBK</page-encoding>
        </jsp-property-group>

        &lt;!&ndash;指定文件内容是否是JSP格式&ndash;&gt;
        <jsp-property-group>
            <url-pattern>*.svg</url-pattern>
            <is-xml>true</is-xml>
        </jsp-property-group>

    </jsp-config>-->
    <!--指定错误的页面 -->
   <!-- <error-page>
        <error-code>404</error-code>
        <location>/NotFound.jsp</location>
    </error-page>

    <error-page>
        <exception-type>exception.ServletNotFound</exception-type>
        <location>/sorry.jsp</location>
    </error-page>-->
</web-app>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@page isELIgnored="false" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>查询商品列表</title>
</head>
<body>
<from action="${pageContext.request.contextPath}/item/queryItem.action" method="post">
    查询条件:
    <table width="100%" border="1">
        <tr>
            <td><input type="submit" value="查询"></td>
        </tr>
    </table>
    商品列表:
    <table width="100%" border="1">
        <tr>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>生产日期</td>
            <td>商品描述</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${itemsList}" var="item">
            <tr>
            <td>${item.name}</td>
            <td>${item.price}</td>
            <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
            <td>${item.detail}</td>

            <td><a href="${pageContext.request.contextPath}/item/queryItem.action?id=${item.id}">修改</a> </td>
            </tr>
        </c:forEach>
    </table>
</from>
</body>
</html>

运行截图:

我的文件目录:

下面是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_2_5.xsd"
         id="WebApp_ID" version="2.5">
 <!-- 配置前端控制器springmvc-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation配置springMVC需要加载的配置文件(配置处理器映射器.适配器....)
    如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)
    这里使用统配符形式配置多个配置文件,如 spring-dao.xml,spring-service.xml,spring-mvc.xml。。。。。

      <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-*.xml</param-value>
    </init-param>
     -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!-- 默认匹配所有的请求 -->
<!--   1. "  *.action    "   访问以.action结尾 的请求,由DispatcherServlet进行解析
   2.   "  /   "        访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析
                 使用该配置可以实现restful方法
   3.   "   /*  "            这样配置不对,改配置,最终要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。-->
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

 springmvc.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.xsd">
 <!--   配置handler-->
    <bean name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>
    <!--处理器映射器-->
   <!-- 将bean的name作为url进行查找,我的bean就是handler,,故配置handler时要指定beanname(就是url),配置内容在上文-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--处理器适配器  所有处理器适配器都实现HandlerAdapter接口-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

   <!-- 视图解析器-->
  <!--  解析jsp,默认使用jstl,classpath下的有jstl的包,视图解析器的有代码://classname     javax.servlet.jsp.jstl.core.Config-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>

ItemsController1 类的代码:

package cn.itcast.ssm.controller;
import cn.itcast.ssm.pojo.Items;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ItemsController1 implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //调用service查找数据库,查询商品列表,这里使用静态数据模拟
        List<Items>itemsList=new ArrayList<Items>();
        Items items_1=new Items();
        items_1.setName("第三方");
        items_1.setPrice(23.3f);
        items_1.setCreatetime(sdf.parse("2018-11-07 09:48:00"));
        items_1.setDetail("iphone受i及");
        Items items_2=new Items();
        items_2.setName("第三方");
        items_2.setPrice(23.3f);
        items_2.setCreatetime(sdf.parse("2018-11-07 09:48:00"));
        items_2.setDetail("iphone受i及");
        itemsList.add(items_1);
        itemsList.add(items_2);
        //返回modelAndView
        ModelAndView modelAndView=new ModelAndView();
        //模型里有视图,这里modelAndView.addObject相当于request的setAttribute,
        // 要在addObject()里指定key,指定值,在jsp中就可以通过键(itemsList)取数据了
        modelAndView.addObject("itemsList",itemsList);
        //指定视图
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
        return modelAndView;
    }
}

itemsList.jsp的代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>查询商品列表</title>
</head>
<body>
<from action="${pageContext.request.contextPath}/item/queryItem.action" method="post">
    查询条件:
    <table width="100%" border="1">
        <tr>
            <td><input type="submit" value="查询"></td>
        </tr>
    </table>
    商品列表:
    <table width="100%" border="1">
        <tr>
            <td>商品名称</td>
            <td>商品价格</td>
            <td>生产日期</td>
            <td>商品描述</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${itemsList}" var="item">
            <tr>
            <td>${item.name}</td>
            <td>${item.price}</td>
            <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
            <td>${item.detail}</td>

            <td><a href="${pageContext.request.contextPath}/item/queryItem.action?id=${item.id}">修改</a> </td>
            </tr>
        </c:forEach>
    </table>
</from>
</body>
</html>

浏览器输入:http://localhost:8080/queryItems.action  会出现上述运行截图

最后总结配置流程:

1.

<!--处理器适配器  所有处理器适配器都实现HandlerAdapter接口-->即Controller
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

2.

 <!-- 视图解析器-->
<!--  解析jsp,默认使用jstl,classpath下的有jstl的包,视图解析器的有代码://classname     javax.servlet.jsp.jstl.core.Config-->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

顺便编写相应jsp的页面

3.

 <!--处理器映射器-->
<!-- 将bean的name作为url进行查找,我的bean就是handler,,故配置handler时要指定beanname(就是url),配置内容在上文-->
 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

4.

<!--   配置handler,即方法的访问路径-->
   <bean name="/queryItems.action" class="cn.itcast.ssm.controller.ItemsController1"/>

猜你喜欢

转载自blog.csdn.net/qq_41063141/article/details/83859861
今日推荐