springMVC简单的实现代码(一)
springMVC可以实现rest风格的链接使用。非常方便1.web.xml
[html]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>01_spring_mvc</display-name>
<!-- springMVC 1.创建servlet -->
<servlet>
<!-- 需要在WEB-INF下面创建对应的hello-servlet.xml -->
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<!-- 捕获所有请求,基于rest -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.hello-servlet.xml
[html]
<?xml version="1.0" encoding="UTF-8"?>
<!-- springMVC 2.创建对应的servlet -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启SpringMVC -->
<mvc:annotation-driven/>
<context:component-scan base-package="cn.edu.zttc.controller"></context:component-scan>
<!-- 配置视图的前缀和后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
3.hello.jsp
[html]
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>hello</title>
</head>
<body>
hello
<br>
${hello}
<br>
${string}
</body>
</html>
4.HelloController.java
[java]
package cn.edu.zttc.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* 基于@的MVC处理
* @author admin
*
*/
@Controller
public class HelloController {
/**
* SpringMVC
* 设定访问规则:@RequestMapping(value={"/","/hello"})
* 传递参数三种方法:
* 1.@RequestParam("userId") int id
* 2.@RequestParam() int id
* 3.int id
* 如果设置了@RequestMapping;则访问的时候必须加入?和参数,否则不会访问当前方法
*
* Struts2的
*
* @param id
* @return
*/
@RequestMapping(value={"/","/hello"})
public String hello(@RequestParam("userId") int id,Map<String, Object> map){
System.out.println("hello");
System.out.println(id);
map.put("hello", "hello world");
return "hello";
}
/**
* 使用Model
* @param model
* @return
*/
// public String say(@RequestParam()int id,Model model){
@RequestMapping(value="/say")
补充:软件开发 , Java ,
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- SSH 导入导出excel 谁有这块的资料吗?
- springmvc 加载一个jsp页面执行多个方法 报404
- 关于用jquery 导入 excel
- java对时间进行循环放到List中
- 一个图片的输入输出程序,第一次调用某方法会中断,第二次调用则正常
- 请上过传智播客的人说一下传智播客怎么样呀!是不是像它说的那样好呀!
- spring的schema怎么配置
- 【菜鸟求助】SSH中怎么从JSP页面往后台传值呢