最近在学习ssm整合,就在练手方案中遇到了一个action访问404的问题。这是我的springmvxc.xml的配置:
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 通过spring组建扫描 -->
<context:component-scan base-package="mz.ssm.controller"/>
<!-- 通过annotation-driven可以替代下边的处理器映射器和适配器 -->
<!-- <mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>-->
<!-- 注解处理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 注解适配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> <!-- 配置视图解析器 要求将jstl的包加到classpath -->
<!-- ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" /> </bean>
</beans>这是web.xml的配置<display-name>ssmproject</display-name>
<!-- 配置spring容器监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置文件的地址 如果不配置contextConfigLocation, 默认查找的配置文件名称classpath下的:servlet名称+"-serlvet.xml"即:springmvc-serlvet.xml -->
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param> </servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 可以配置/ ,此工程 所有请求全部由springmvc解析,此种方式可以实现 RESTful方式,需要特殊处理对静态文件的解析不能由springmvc解析
可以配置*.do或*.action,所有请求的url扩展名为.do或.action由springmvc解析,此种方法常用 不可以/*,如果配置/*,返回jsp也由springmvc解析,这是不对的。 -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>这是我controller的部分:@Controller
public class ItemsController {
//注入service
private ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems( ) throws Exception{
List<ItemsCustom> itemsList=itemsService.findItemsList(null);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemsList",itemsList);
//指定逻辑视图名
modelAndView.setViewName("itemsList");
return modelAndView;
}
}
这是我的程序结构
我想通过访问queryitems.action访问webweb-inf/jsp/下的itemslist.jsp却总是提示开头所提到的那个页面。
不知道什么情况什么问题。
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 通过spring组建扫描 -->
<context:component-scan base-package="mz.ssm.controller"/>
<!-- 通过annotation-driven可以替代下边的处理器映射器和适配器 -->
<!-- <mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>-->
<!-- 注解处理器映射器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 注解适配器 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> <!-- 配置视图解析器 要求将jstl的包加到classpath -->
<!-- ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" /> </bean>
</beans>这是web.xml的配置<display-name>ssmproject</display-name>
<!-- 配置spring容器监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载springmvc配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 配置文件的地址 如果不配置contextConfigLocation, 默认查找的配置文件名称classpath下的:servlet名称+"-serlvet.xml"即:springmvc-serlvet.xml -->
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param> </servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 可以配置/ ,此工程 所有请求全部由springmvc解析,此种方式可以实现 RESTful方式,需要特殊处理对静态文件的解析不能由springmvc解析
可以配置*.do或*.action,所有请求的url扩展名为.do或.action由springmvc解析,此种方法常用 不可以/*,如果配置/*,返回jsp也由springmvc解析,这是不对的。 -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>这是我controller的部分:@Controller
public class ItemsController {
//注入service
private ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems( ) throws Exception{
List<ItemsCustom> itemsList=itemsService.findItemsList(null);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemsList",itemsList);
//指定逻辑视图名
modelAndView.setViewName("itemsList");
return modelAndView;
}
}
这是我的程序结构
我想通过访问queryitems.action访问webweb-inf/jsp/下的itemslist.jsp却总是提示开头所提到的那个页面。
不知道什么情况什么问题。
在这加个.action试试
@RequestMapping("/book.do")
public class BookController {
private BookService bookService; @RequestMapping(params = "method=add")
public String add(Book book) {
System.out.println("bookname:" + book.getName());
System.out.println("author:" + book.getAuthor());
bookService.add(book);
return "success";
} @RequestMapping(params = "method=update")
public String update(Book book) {
bookService.update(book);
return "success";
} public BookService getBookService() {
return bookService;
} @Resource
public void setBookService(BookService bookService) {
this.bookService = bookService;
}}
前台访问<form method="post"
action="<%=request.getContextPath()%>/book.do?method=add">
bookname:<input type="text" name="name" id="name"> author:<input
type="text" name="author" id="author"> <input type="submit"
value="Add">
</form>
@RequestMapping("/book.do")
public class BookController {
private BookService bookService; @RequestMapping(params = "method=add")
public String add(Book book) {
System.out.println("bookname:" + book.getName());
System.out.println("author:" + book.getAuthor());
bookService.add(book);
return "success";
} @RequestMapping(params = "method=update")
public String update(Book book) {
bookService.update(book);
return "success";
} public BookService getBookService() {
return bookService;
} @Resource
public void setBookService(BookService bookService) {
this.bookService = bookService;
}}
前台访问<form method="post"
action="<%=request.getContextPath()%>/book.do?method=add">
bookname:<input type="text" name="name" id="name"> author:<input
type="text" name="author" id="author"> <input type="submit"
value="Add">
</form>
你好请问怎么判断有没有进action?
@RequestMapping("/book.do")
public class BookController {
private BookService bookService; @RequestMapping(params = "method=add")
public String add(Book book) {
System.out.println("bookname:" + book.getName());
System.out.println("author:" + book.getAuthor());
bookService.add(book);
return "success";
} @RequestMapping(params = "method=update")
public String update(Book book) {
bookService.update(book);
return "success";
} public BookService getBookService() {
return bookService;
} @Resource
public void setBookService(BookService bookService) {
this.bookService = bookService;
}}
前台访问<form method="post"
action="<%=request.getContextPath()%>/book.do?method=add">
bookname:<input type="text" name="name" id="name"> author:<input
type="text" name="author" id="author"> <input type="submit"
value="Add">
</form>
你好请问怎么判断有没有进action?
在你要执行的那个方法里打印一句话,或者打个断点,然后执行一下就能看出来
你可以在后台要进的方法打个断点看看有没有跳进来,另外把你的页面代码提上来,看看是不是提交的路径写的有问题也说不准的
你可以在后台要进的方法打个断点看看有没有跳进来,另外把你的页面代码提上来,看看是不是提交的路径写的有问题也说不准的
这是我的访问路径。
你可以在后台要进的方法打个断点看看有没有跳进来,另外把你的页面代码提上来,看看是不是提交的路径写的有问题也说不准的
这是我的访问路径。
localhost:8080 这个是不是漏了?
你可以在后台要进的方法打个断点看看有没有跳进来,另外把你的页面代码提上来,看看是不是提交的路径写的有问题也说不准的
这是我的访问路径。
localhost:8080 这个是不是漏了?
有的。没有漏。
你可以在后台要进的方法打个断点看看有没有跳进来,另外把你的页面代码提上来,看看是不是提交的路径写的有问题也说不准的
这是我的访问路径。
localhost:8080 这个是不是漏了?
有的。没有漏。
测试代码发我一份?
我看你controller的service都没有注入,
把@Autowired加上试试 //注入service
@Autowired
private ItemsService itemsService;
两个@ReauestMappering("/xxx")
一个加在类上,另一个加在方法上
</mvc:annotation-driven>-->
不是应该在 web-inf 目录下创建 spring目录的吗??