有谁用过spring mvc框架

synized 2012-02-22 03:17:58
想从JSP页面传递参数到controller 但是不知道action该怎么写,controller在spring-servlet.xml中该怎么配置才能正确的得到从这个JSP用POST请求传递的参数
...全文
136 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Richard2018 2012-02-23
  • 打赏
  • 举报
回复
...
cjoy4856 2012-02-23
  • 打赏
  • 举报
回复
看看这个吧http://blog.csdn.net/cjoy4856/article/details/5600173
24K純帥 2012-02-22
  • 打赏
  • 举报
回复
spring..在controller ModelAndView,注解的方式或者在xml配置
  • 打赏
  • 举报
回复
别人给的示例...你参考一下吧
一个简单的基于注解的 Controller
使用过低版本 Spring MVC 的读者都知道:当创建一个 Controller 时,我们需要直接或间接地实现 org.springframework.web.servlet.mvc.Controller 接口。一般情况下,我们是通过继承 SimpleFormController 或 MultiActionController 来定义自己的 Controller 的。在定义 Controller 后,一个重要的事件是在 Spring MVC 的配置文件中通过 HandlerMapping 定义请求和控制器的映射关系,以便将两者关联起来。
来看一下基于注解的 Controller 是如何定义做到这一点的,下面是使用注解的 BbtForumController:

清单 1. BbtForumController.java


import com.baobaotao.service.BbtForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Collection;

@Controller //<——①
@RequestMapping("/forum.do")
public class BbtForumController {

@Autowired
private BbtForumService bbtForumService;

@RequestMapping //<——②
public String listAllBoard() {
bbtForumService.getAllBoard();
System.out.println("call listAllBoard method.");
return "listBoard";
}
}

从上面代码中,我们可以看出 BbtForumController 和一般的类并没有区别,它没有实现任何特殊的接口,因而是一个地道的 POJO。让这个 POJO 与众不同的魔棒就是 Spring MVC 的注解!
在 ① 处使用了两个注解,分别是 @Controller 和 @RequestMapping。在“使用 Spring 2.5 基于注解驱动的 IoC”这篇文章里,笔者曾经指出过 @Controller、@Service 以及 @Repository 和 @Component 注解的作用是等价的:将一个类成为 Spring 容器的 Bean。由于 Spring MVC 的 Controller 必须事先是一个 Bean,所以 @Controller 注解是不可缺少的。
真正让 BbtForumController 具备 Spring MVC Controller 功能的是 @RequestMapping 这个注解。@RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来;还可以标注在方法签名处,以便进一步对请求进行分流。在 ① 处,我们让 BbtForumController 关联“/forum.do”的请求,而 ② 处,我们具体地指定 listAllBoard() 方法来处理请求。所以在类声明处标注的 @RequestMapping 相当于让 POJO 实现了 Controller 接口,而在方法定义处的 @RequestMapping 相当于让 POJO 扩展 Spring 预定义的 Controller(如 SimpleFormController 等)。
为了让基于注解的 Spring MVC 真正工作起来,需要在 Spring MVC 对应的 xxx-servlet.xml 配置文件中做一些手脚。在此之前,还是先来看一下 web.xml 的配置吧:

清单 2. web.xml:启用 Spring 容器和 Spring MVC 框架

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Spring Annotation MVC Sample</display-name>
<!-- Spring 服务层的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Spring 容器启动监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<!-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的
配置文件,以启动Spring MVC模块-->
<servlet>
<servlet-name>annomvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>annomvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示:
清单 3. annomvc-servlet.xml

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.baobaotao.web"/>

<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.
AnnotationMethodHandlerAdapter"/>

<!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>

因为 Spring 所有功能都在 Bean 的基础上演化而来,所以必须事先将 Controller 变成 Bean,这是通过在类中标注 @Controller 并在 annomvc-servlet.xml 中启用组件扫描机制来完成的,如 ① 所示。
在 ② 处,配置了一个 AnnotationMethodHandlerAdapter,它负责根据 Bean 中的 Spring MVC 注解对 Bean 进行加工处理,使这些 Bean 变成控制器并映射特定的 URL 请求。
而 ③ 处的工作是定义模型视图名称的解析规则,这里我们使用了 Spring 2.5 的特殊命名空间,即 p 命名空间,它将原先需要通过 <property> 元素配置的内容转化为 <bean> 属性配置,在一定程度上简化了 <bean> 的配置。
启动 Tomcat,发送 http://localhost/forum.do URL 请求,BbtForumController 的 listAllBoard() 方法将响应这个请求,并转向 WEB-INF/jsp/listBoard.jsp 的视图页面。
yugaochao 2012-02-22
  • 打赏
  • 举报
回复
/**

* @param request
* @param response
* @return
* @throws Exception
*/
public ModelAndView abc(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.getParameter("");
return new ModelAndView("");
}
直接request里面取
synized 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xt_yangjie 的回复:]

楼主,

你也太懒了,这种东西自己上网搜搜就知道该如何配置了.
[/Quote]不知其所以然呀
xt_yangjie 2012-02-22
  • 打赏
  • 举报
回复
楼主,

你也太懒了,这种东西自己上网搜搜就知道该如何配置了.
synized 2012-02-22
  • 打赏
  • 举报
回复
忘了 我说的是spring mvc3
zysnba 2012-02-22
  • 打赏
  • 举报
回复
@Controller
@RequestMapping("/teting")

@RequestMapping(params = "method=getLogin")
public String getLogin(HttpServletRequest request){

}
synized 2012-02-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 goldenfish1919 的回复:]

http://www.iteye.com/topic/973918
[/Quote]不是很给力的说 配置都没有贴出来
若鱼1919 2012-02-22
  • 打赏
  • 举报
回复
http://www.iteye.com/topic/973918

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧