Spring mvc 关于spring的url映射的简单问题
我使用Spring4.1进行开发。我尝试将我的URL映射到控制器 下面是我的web.xml文件的摘录Spring mvc 关于spring的url映射的简单问题,spring-mvc,Spring Mvc,我使用Spring4.1进行开发。我尝试将我的URL映射到控制器 下面是我的web.xml文件的摘录 <!-- Spring MVC --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class&
<!-- 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>
mvc调度器
org.springframework.web.servlet.DispatcherServlet
1.
mvc调度器
/*
下面是我的SpringXML文件的摘录
<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" />
/WEB-INF/pages/
.jsp
这是我的控制器文件的摘录
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;
}
}
包web.controller.impl;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入org.apache.logging.log4j.LogManager;
导入org.apache.logging.log4j.Logger;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.servlet.ModelAndView;
进口服务.公共管理服务;
导入web.view.QuestionItem;
导入域。问题;
@控制器
公共类组合控制器{
私有静态最终记录器Logger=LogManager.getLogger();
@自动连线
@限定符(“commonManagementService”)
私人公共管理服务公共管理服务;
公共公共管理服务getCommonManagementService(){
返回此.commonManagementService;
}
@RequestMapping(value=“/unsecure/getQuestion”,method=RequestMethod.GET)
公共模型和视图getQuestion(模型映射模型){
LOGGER.info(“首次登场方法getQuestion”);
最终列表结果=新建ArrayList();
最终问题列表=this.commonManagementService.getQuestions();
(最后一个问题:问题){
添加(新的问题项(question.getQuestion(),question.getId().toString());
}
final-Map-modelToPass=new-HashMap();
模型。投入(“项目”,结果);
LOGGER.info(“fin methode getQuestion”);
返回新模型和视图(“jsonResultView”,模型);
}
公共管理服务(
公共管理服务(公共管理服务){
this.commonManagementService=commonManagementService;
}
}
我的应用程序名为tennisArc1600,当我尝试发送url时
或
我得到错误404,ressource不可用
提前感谢您的建议在web.xml中使用
<url-pattern>/</url-pattern>
/
然后申报
<mvc:annotation-driven/>
或者
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
在您的SpringWeb配置中您好,请测试Testini,谢谢您的回答 我成功地找到了我的控制器。这是我的配置 web.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>
mvc调度器
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/mvc-dispatcher-servlet.xml
1.
mvc调度器
/
mvc-dispatcher-servlet.xml
<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>
类路径:messages.properties
类路径:persistence.properties
类路径:securities.properties
类路径:mail.properties
类路径:views.properties
/WEB-INF/pages/
.jsp
我的控制器
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 utils.Result;
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.htm", 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);
model.put("results", new ArrayList<Result>());
LOGGER.info("fin methode getQuestion");
return new ModelAndView("jsonResultView", model);
}
public void setCommonManagementService(
CommonManagementService commonManagementService) {
this.commonManagementService = commonManagementService;
}
}
包web.controller.impl;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入org.apache.logging.log4j.LogManager;
导入org.apache.logging.log4j.Logger;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.servlet.ModelAndView;
进口服务.公共管理服务;
导入utils.Result;
导入web.view.QuestionItem;
导入域。问题;
@控制器
公共类组合控制器{
私有静态最终记录器Logger=LogManager.getLogger();
@自动连线
@限定符(“commonManagementService”)
私人公共管理服务公共管理服务;
公共公共管理服务getCommonManagementService(){
返回此.commonManagementService;
}
@RequestMapping(value=“/unsecure/getQuestion.htm”,method=RequestMethod.GET)
公共模型和视图getQuestion(模型映射模型){
LOGGER.info(“首次登场方法getQuestion”);
最终列表结果=新建ArrayList();
最终问题列表=this.commonManagementService.getQuestions();
(最后一个问题:问题){
添加(新的问题项(question.getQuestion(),question.getId().toString());
}
final-Map-modelToPass=new-HashMap();
模型。投入(“项目”,结果);
model.put(“results”,newarraylist());
LOGGER.info(“fin methode getQuestion”);
返回新模型和视图(“jsonResultView”,模型);
}
公共管理服务(
公共管理服务(公共管理服务){
this.commonManagementService=commonManagementService;
}
}
再次感谢hi@flamant,欢迎您,一般来说,如果您认为某个答案解决了您的问题,您应该接受它作为问题答案。