显示Tomcat8 Spring MVC4 Maven JSP源代码而不是内容 [英] Tomcat8 Spring MVC4 maven jsp source code displayed instead of content

查看:75
本文介绍了显示Tomcat8 Spring MVC4 Maven JSP源代码而不是内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从My MacBook Air构建和部署Spring MVC Web应用程序.创建了maven项目,并添加了该项目的所有必需文件.能够构建项目并通过maven pom.xml部署到Tomcat 8.当我访问该页面时,它只会向我显示jsp代码而不是内容.

I am trying to build and deploy the Spring MVC web application from My MacBook Air. Created the maven project and added all the necessary files for the project. Able to build the project and deploy to Tomcat 8 via maven pom.xml. When I access the page it just shows me the jsp code as it is instead of content.

我无法在tomcat工作文件夹中看到生成的servlet文件.由于正在通过Maven部署文件,因此我认为它无法隐藏在我的Eclipse工作区中.

I could not able to see the generated servlet file out tomcat work folder. Since am deploying the files via maven I don't think it can hide in my eclipse workspace.

我也遇到了类似的问题,但是没有一个人完成表格.例如,有一些建议在servlet.xml中的WEB -INF前面添加/,并在JSP中添加标签,并将内容放在body标签内部.我希望这与我的Tomcat8和Maven3.3.9有关.

I went through similar questions but none of them worked out the form. For example, there were some suggestions to add / in front of WEB -INF in servlet.xml and add the tags in JSP and also put the content inside of the body tag. I hope this is something to do with my Tomcat8 and Maven3.3.9

当我使用URL访问应用程序时,我可以看到控制器内部的日志,这意味着我的代码已部署到Tomcat中,但仅发生了从JSP到servlet的转换.

When I access the application with URL, I can see the logs inside of my controller so it means that my code is deployed into Tomcat but only JSP to servlet conversion is not happening.

这是我的配置文件.

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">
    <display-name>Digital Services</display-name>

    <servlet>
        <servlet-name>digitalservices</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/digitalservices-servlet.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>digitalservices</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

ditalservices-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:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    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.2.xsd 
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.rajan.myservices.controller" />

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"
        id="tilesViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.tiles3.TilesView" />
    </bean>
    -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

Maven POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.rajan.myservices</groupId>
    <artifactId>DigitalServices</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>DigitalServices Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <springframework.version>4.0.3.RELEASE</springframework.version>
        <apachetiles.version>3.0.5</apachetiles.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Below declared dependencies are included for the servers who may complain 
            about servlet/jstl missing dependency -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>DigitalServices</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://127.0.0.1:8080/manager/text</url>
                    <server>TomcatServer</server>
                    <username>tomcat</username>
                    <password>tomcat1</password>
                    <path>/DigitalServices</path>
                    <update>true</update>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        </plugins>
    </build>
</project>

控制器类

package com.rajan.myservices.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rajan.myservices.model.Person;


@Controller
public class HomePageController {

    @RequestMapping(value="index")
    public String index() {
        System.out.println("In index");
        return "index";
    }

    @RequestMapping(value="viewPerson")
    public ModelAndView viewPersons(Model model) {
        Map<String, List<Person>> persons = 
                new HashMap<String, List<Person>>();
        System.out.println("In View Person");
        persons.put("persons", Person.createPersons());
        System.out.println("persons: "+persons);
        return new ModelAndView("personList", persons);
    }

}

Index.jsp

<html>
<body>
<div style="margin:10px;">
    <h3>Welcome to Digital Services by <% out.println("Hello Test"); %></h3>
</div>
</body>
</html>

使用 http://localhost:8080/DigitalServices/index来访问应用程序时的Jsp输出

Jsp output when acccess the application using http://localhost:8080/DigitalServices/index

Welcome to Digital Services by <% out.println("Hello Test"); %>

仅供参考-我可以在catalina.out中看到来自Controller类的那些sysout.

FYI - I can see those sysouts from Controller class in catalina.out.

推荐答案

您在JSP文件中使用的是纯HTML,因此摘要无法编译.您缺少页面指令. page指令用于向容器提供与当前JSP页面有关的指令.您可以在JSP页面中的任何位置编写页面指令.按照惯例,页面指令在JSP页面的顶部进行编码.

You are using pure HTML in JSP file so snippets won't compile. You are missing page directive. The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code the page directives anywhere in your JSP page. By convention, page directives are coded at the top of the JSP page.

以下是页面指令的基本语法-<%@ page attribute = "value" %>

Following is the basic syntax of page directive − <%@ page attribute = "value" %>

 <%@ 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>Insert title here</title>
    </head>
    <body>
     <div style="margin:10px;">
        <h3>Welcome to Digital Services by <% out.println("Hello Test"); %></h3>
     </div>
    </body>
    </html>

这篇关于显示Tomcat8 Spring MVC4 Maven JSP源代码而不是内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆