【发布时间】:2014-07-15 08:08:30
【问题描述】:
我是 Spring MVC 的新手,我正在转换我的标准 Web Java 项目。我在使项目默认调用控制器而不是 JSP 页面时遇到问题。在每个请求都由控制器处理之前的 Web 项目中,Spring 似乎并非如此,有人可以建议吗?
我正在使用 Netbeans,到目前为止我的代码如下:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/login</welcome-file>
</welcome-file-list>
调度服务小程序
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.danny" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/JSP/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/styles/**" location="/styles/" />
<mvc:annotation-driven />
登录控制器
package com.spring.InternetJavaSpring.Controllers;
import com.spring.InternetJavaSpring.BusinessLogic.UserBusLog;
import static com.spring.InternetJavaSpring.BusinessLogic.UserBusLog.currentUser;
import com.spring.InternetJavaSpring.Model.Login;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="login")
public class LoginCont {
@RequestMapping(method = RequestMethod.GET)
public String login(Map<String, Object>model) {
Login user = new Login();
model.put("loginForm", user);
return "login";
}
@RequestMapping(method = RequestMethod.POST)
public String loginUser(@Valid @ModelAttribute("loginForm")
Login xLogin, BindingResult xResult, Map<String, Object>model) {
if(xResult.hasErrors()){
return "login";
}
UserBusLog ULogic = new UserBusLog();
ULogic.Login(xLogin);
model.put("fname", currentUser.getFName());
model.put("lname", currentUser.getLName());
return "home";
}
任何帮助都将不胜感激,因为我已经花了两天时间在这方面,但无处可去。非常感谢
【问题讨论】:
-
你用来点击控制器的 URL 是什么,你得到的错误信息是什么?
-
localhost/SpringIJ/login 是我希望它默认调用的,我得到一个 404
-
下面的回答有效果吗?
标签: spring spring-mvc model-view-controller controller dispatcher