我是 Spring MVC 的新手,并试图从一些网络教程和 Spring 的 PetClinic 中学习。当我刚开始时,我正在使用一些来自 Spring 的一些非常旧版本的教程。当时我不知道,所以它已经有很多回来和重新组合的尝试。我的测试站点查找 Controller 类的方式有问题。该站点找到了我的 index.jsp 文件并执行得很好。但是,当我尝试单击映射到 Controller 类之一的链接时,我得到一个找不到页面 (404) 错误。
一切都在 JBoss EAP 6.1 上编译和部署,没有错误。我附上了我的配置文件中的一些片段。有人可以看看他们,看看我做错了什么>
Web.xml 片段:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:resources/spring/core-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
调度程序 servlet.xml 片段:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean class="org.S2Me.MyHealth.controller.CustomMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.servlet.PageNotFound">notFound</prop>
<prop key="java.lang.Exception">failure</prop>
</props>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
>
<property name="basename" value="/WEB-INF/messages" />
</bean>
核心 Servlet 片段:
<import resource="view-config.xml"/>
<context:component-scan base-package="org.S2Me.MyHealth.controller" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
<mvc:view-controller path="/" view-name="index" />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages/messages" />
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"
>
<property name="defaultErrorView" value="exception" />
<!-- results into 'WEB-INF/jsp/exception.jsp' -->
<property name="warnLogCategory" value="warn" />
</bean>
view-Servlet 片段:
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
>
<property name="contentNegotiationManager" ref="cnManager" />
<property name="viewResolvers">
<list>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean
class="org.springframework.web.servlet.view.BeanNameViewResolver" />
</list>
</property>
</bean>
<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true"/>
<property name="ignoreAcceptHeader" value="true"/>
<property name="defaultContentType" value="text/html"/>
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
<entry key="atom" value="application/atom+xml" />
</map>
</property>
</bean>
index.jsp 片段:
<div class="navbar-inner">
<ul class="nav">
<li style="width: 100px;"><a href="<spring:url value="/" htmlEscape="true" />"><i class="icon-home"></i>
Home</a></li>
<li style="width: 130px;">
<a href="<spring:url value="/login" htmlEscape="true" />"> Login</a>
</li>
</ul>
</div>
登录控制器类:
package org.S2Me.MyHealth.controller;
import org.S2Me.MyHealth.server.LoginForm;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.validation.BindingResult;
import java.util.Map;
import javax.validation.Valid;
@Controller
@RequestMapping("/login")
public class LoginController
{
@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping(method = RequestMethod.GET)
public String showForm(Map model)
{
LoginForm loginForm = new LoginForm();
model.put("loginForm", loginForm);
return "loginform";
}
@RequestMapping(method = RequestMethod.POST)
public String processForm(@Valid LoginForm loginForm, BindingResult result,
Map model)
{
String userName = "UserName";
String password = "password";
if (result.hasErrors())
{
return "loginform";
}
loginForm = ( LoginForm) model.get("loginForm");
if (!loginForm.getUserName().equals(userName)
|| !loginForm.getPassword().equals(password))
{
return "loginform";
}
model.put("loginForm", loginForm);
return "loginsuccess";
}
}
任何语法错误都是我的剪切和粘贴,就像我说编译或部署没有错误,只是导航时出现 404 错误。任何帮助,将不胜感激。谢谢