我用4.1开发了。我尝试用控制器映射我的网址
以下是我的web.xml文件的摘录
<!-- Spring MVC -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
这是我的spring xml文件的摘录
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:component-scan base-package="orm,orm.impl,web.controller.impl,web.view" />
以下是我的控制器文件的摘录
package web.controller.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import service.CommonManagementService;
import web.view.QuestionItem;
import domain.Question;
@Controller
public class ComboController {
private static final Logger LOGGER = LogManager.getLogger();
@Autowired
@Qualifier("commonManagementService")
private CommonManagementService commonManagementService;
public CommonManagementService getCommonManagementService() {
return this.commonManagementService;
}
@RequestMapping(value = "/unsecure/getQuestion", method = RequestMethod.GET)
public ModelAndView getQuestion(ModelMap model) {
LOGGER.info("debut methode getQuestion");
final List<QuestionItem> results = new ArrayList<QuestionItem>();
final List<Question> questions = this.commonManagementService.getQuestions();
for (final Question question : questions) {
results.add(new QuestionItem(question.getQuestion(), question.getId().toString()));
}
final Map<String, Object> modelToPass = new HashMap<String, Object>();
model.put("items", results);
LOGGER.info("fin methode getQuestion");
return new ModelAndView("jsonResultView", model);
}
public void setCommonManagementService(
CommonManagementService commonManagementService) {
this.commonManagementService = commonManagementService;
}
}
我的应用程序的名称是tennisArc1600,当我尝试发送网址时 http://localhost:8080/tennisArc1600/unsecure/getQuestion或http://localhost:8080/unsecure/getQuestion 我得到错误404,资源不可用
提前感谢您的建议
答案 0 :(得分:0)
在web.xml中使用
<url-pattern>/</url-pattern>
然后宣布
<mvc:annotation-driven/>
或者
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
在您的Spring Web配置中
答案 1 :(得分:0)
Hello Test Testini,谢谢你的回答。
我成功接触了我的控制器。这是我的配置
的web.xml
def naiveTokenizer(text: String): Array[String] = text.split("""\s+""")
def naiveWordCount(text: String): Int = naiveTokenizer(text).length
val lines = sc.textFile("README.md")
lines.filter(naiveWordCount(_) >= 3)
MVC-调度-servlet.xml中
<!-- Spring MVC -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我的控制器
<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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- <context:component-scan base-package="domain,orm,orm.impl" /> -->
<bean
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:messages.properties</value>
<value>classpath:persistence.properties</value>
<value>classpath:securities.properties</value>
<value>classpath:mail.properties</value>
<value>classpath:views.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="abstractViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views"/>
<property name="order" value="0"/>
</bean>
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/" cache-period="10000" />
<mvc:annotation-driven />
<context:component-scan base-package="orm,orm.impl,web.controller.impl,web.view" />
</beans>
再次感谢