错误的网址模式?页面不会在tomcat中打开

时间:2022-03-17 06:50:14

The spring/hibernate web application that I am creating is building and deploying correctly but I am having some problems when trying to access the jsp pages inside my WEB-INF/jsp folder. The test jsp page that I have placed in my WebContent folder opens correctly.

我正在创建的spring / hibernate Web应用程序正在构建和正确部署,但是在尝试访问WEB-INF / jsp文件夹中的jsp页面时遇到了一些问题。我放在WebContent文件夹中的测试jsp页面正确打开。

Here is my code:

这是我的代码:

new-customer.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
</head>
<body>

    <h1>Create New Customer</h1>

    <c:url var="saveUrl" value="/mis/start/newcustomer" />
    <form:form modelAttribute="customerAttribute" method="POST" action="${saveUrl}">
        <table>
            <tr>
                <td><form:label path="customerTargetId">Customer Target ID:</form:label></td>
                <td><form:input path="customerTargetId"/></td>
            </tr>

            <tr>
                <td><form:label path="customerName">Customer Name</form:label></td>
                <td><form:input path="customerName"/></td>
            </tr>

            <tr>
                <td><form:label path="customerCountry">Customer Country</form:label></td>
                <td><form:input path="customerCountry"/></td>
            </tr>
        </table>

        <input type="submit" value="Save" />
    </form:form>

</body>
</html>

start.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Test</h1>


<c:url var="addUrl" value="/mis/start/newcustomer" />
<p><a href="${addUrl}">Create new customer</a></p>
</body>
</html>

CustomerController

package testapp.mis.controller;

@Controller
@RequestMapping("/start")
public class CustomerController {

@Resource(name="customerService")
private CustomerService customerService;

@RequestMapping(value="/newcustomer", method=RequestMethod.GET)
public String getCustomer(Model model) {

    model.addAttribute("customerAttribute", new Customer());

    return "new-customer";
}

@RequestMapping(value="/newcustomer", method=RequestMethod.POST)
public String postCustomer(@ModelAttribute("customerAttribute") Customer customer) {
    customerService.createCustomer(customer);

    return "redirect:/mis/start";
}
}

web.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app id="WebApp_ID" 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">

     <servlet>
     <servlet-name>mis</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
     </servlet>

<servlet-mapping>
<servlet-name>mis</servlet-name>
<url-pattern>/mis/*</url-pattern>
</servlet-mapping>


    </web-app>

When I for example try to go to http://localhost/MIS/mis/start it tells me the page does not exist. (I have tried all combinations of localhost/MIS/start, localhost/mis/start/newcustomer etc.)

例如,当我尝试转到http:// localhost / MIS / mis / start时,它告诉我该页面不存在。 (我已经尝试过localhost / MIS / start,localhost / mis / start / newcustomer等的所有组合)

Can anyone see what the problem is?

任何人都可以看到问题是什么?

Let me know if you need some other part of my code to help. Thank you!

如果您需要我的代码的其他部分来帮助我,请告诉我。谢谢!

Edit:

adding the other config files:

添加其他配置文件:

applicationContext.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: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">


<context:annotation-config />
<context:component-scan base-package="testapp.mis" />
<mvc:annotation-driven />
<import resource="hibernate-context.xml" />

</beans>

hibernate-context.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-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/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
  ">

<context:property-placeholder location="/WEB-INF/spring.properties" />

<!-- Enables annotations for transaction management -->
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    p:dataSource-ref="dataSource"
    p:configLocation="${hibernate.config}"
    p:packagesToScan="testapp.mis"/>

<!-- Declare a datasource that has pooling capabilities--> 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${app.jdbc.driverClassName}"
p:jdbcUrl="${app.jdbc.url}"
p:user="${app.jdbc.username}"
p:password="${app.jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />

<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
      p:sessionFactory-ref="sessionFactory" />

</beans> 

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<!-- We're using MySQL database so the dialect needs to MySQL as well-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- Enable this to see the SQL statements in the logs-->
<property name="show_sql">false</property>

<!-- Remove after testing -->

<!-- This will drop our existing database and re-create a new one.
  Existing data will be deleted! -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>

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

<!-- Declare a view resolver -->
<bean id="viewResolver"   class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

spring.properties:

# database properties
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost/test
app.jdbc.username=testapp
app.jdbc.password=testapp

#hibernate properties
hibernate.config=/WEB-INF/hibernate.cfg.xml

2 个解决方案

#1


0  

Since I can't comment, I'll have to write my question as an answer (which it might be):

由于我无法发表评论,我将不得不将我的问题写成答案(可能是这样):

Do you have your spring.xml setup correctly? The controller needs to be picked up as a bean and as I remember, you need to tell Spring which packages to scan.

你有正确的spring.xml设置吗?控制器需要被拿起来作为一个bean,我记得,你需要告诉Spring要扫描哪些软件包。

<?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:sws="http://www.springframework.org/schema/web-services"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="your.controller.package"/>
</beans>

#2


0  

My original assumption that it was building correctly was wrong, my java files weren't getting placed in the right folder. This caused the classes folder under WEB-INF where tomcat expected them to be to be empty. Here is the ant build file that is working correctly:

我最初假设它正确构建是错误的,我的java文件没有放在正确的文件夹中。这导致了WEB-INF下的classes文件夹,其中tomcat期望它们为空。这是正确运行的ant构建文件:

<?xml version="1.0"?>

<project name="Test" default="build" basedir=".">

    <!-- Configure the build properties file which contains properties to access the Manager application -->
    <property file="build.properties"/>

    <!-- Configure web directory -->
    <property name="web.dir" value="WebContent"/>


    <!-- Configure the directory into which the web application is built -->
    <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>

    <!-- Configure source directory that contains the java code -->
    <property name="src.dir" value="src"/>


    <!--Configure the context path for the application -->
    <property name="path" value="/test"/>

    <!-- Add all the lib files inside the WebContent/WEB-INF/lib directory as well as the tomcat lib files to the classpath -->
    <path id="master-classpath">
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${appserver.lib}">
            <!-- Apache Tomcat lib. ${appserver.lib} comes from the build.properties file -->
            <include name="servlet*.jar"/>
        </fileset>
        <pathelement path="${build.dir}"/>
    </path>

    <!-- Executable Targets -->
    <target name="init" description="Creates build directory">
        <mkdir dir="${build.dir}"/>
    </target>

    <target name="build" depends="init" description="Compiles the java files in the main source tree">
        <javac destdir="${build.dir}" debug="true" srcdir="${src.dir}">
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <target name="war" depends="build" description="Creates a war file in the tomcat directory">
        <war destfile="${appserver.home}/webapps/${path}.war" webxml="${web.dir}/WEB-INF/web.xml">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </war>
    </target>
</project>

#1


0  

Since I can't comment, I'll have to write my question as an answer (which it might be):

由于我无法发表评论,我将不得不将我的问题写成答案(可能是这样):

Do you have your spring.xml setup correctly? The controller needs to be picked up as a bean and as I remember, you need to tell Spring which packages to scan.

你有正确的spring.xml设置吗?控制器需要被拿起来作为一个bean,我记得,你需要告诉Spring要扫描哪些软件包。

<?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:sws="http://www.springframework.org/schema/web-services"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="your.controller.package"/>
</beans>

#2


0  

My original assumption that it was building correctly was wrong, my java files weren't getting placed in the right folder. This caused the classes folder under WEB-INF where tomcat expected them to be to be empty. Here is the ant build file that is working correctly:

我最初假设它正确构建是错误的,我的java文件没有放在正确的文件夹中。这导致了WEB-INF下的classes文件夹,其中tomcat期望它们为空。这是正确运行的ant构建文件:

<?xml version="1.0"?>

<project name="Test" default="build" basedir=".">

    <!-- Configure the build properties file which contains properties to access the Manager application -->
    <property file="build.properties"/>

    <!-- Configure web directory -->
    <property name="web.dir" value="WebContent"/>


    <!-- Configure the directory into which the web application is built -->
    <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>

    <!-- Configure source directory that contains the java code -->
    <property name="src.dir" value="src"/>


    <!--Configure the context path for the application -->
    <property name="path" value="/test"/>

    <!-- Add all the lib files inside the WebContent/WEB-INF/lib directory as well as the tomcat lib files to the classpath -->
    <path id="master-classpath">
        <fileset dir="${web.dir}/WEB-INF/lib">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="${appserver.lib}">
            <!-- Apache Tomcat lib. ${appserver.lib} comes from the build.properties file -->
            <include name="servlet*.jar"/>
        </fileset>
        <pathelement path="${build.dir}"/>
    </path>

    <!-- Executable Targets -->
    <target name="init" description="Creates build directory">
        <mkdir dir="${build.dir}"/>
    </target>

    <target name="build" depends="init" description="Compiles the java files in the main source tree">
        <javac destdir="${build.dir}" debug="true" srcdir="${src.dir}">
            <classpath refid="master-classpath"/>
        </javac>
    </target>

    <target name="war" depends="build" description="Creates a war file in the tomcat directory">
        <war destfile="${appserver.home}/webapps/${path}.war" webxml="${web.dir}/WEB-INF/web.xml">
            <fileset dir="${web.dir}">
                <include name="**/*.*"/>
            </fileset>
        </war>
    </target>
</project>