- 浏览: 41472 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
zph999:
damoo 写道不错,看了你的也解决了我的问题。谢谢。例子能透 ...
关于Java生成背景透明的png图片 -
damoo:
不错,看了你的也解决了我的问题。谢谢。
关于Java生成背景透明的png图片
web.xml 配置:
1. <servlet>
2. <servlet-name>dispatcher</servlet-name>
3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4. <init-param>
5. <description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC 的配置文件</description>
6. <param-name>contextConfigLocation</param-name>
7. <param-value>/WEB-INF/spring-mvc/*.xml</param-value>
8. </init-param>
9. <load-on-startup>1</load-on-startup>
10. </servlet>
11. <servlet-mapping>
12. <servlet-name>dispatcher</servlet-name>
13. <url-pattern>*.htm</url-pattern>
14. </servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
这样,所有的.htm的请求,都会被DispatcherServlet处理;
初始化 DispatcherServlet 时,该框架在 web 应用程序WEB-INF 目录中寻找一个名为[servlet-名称]-servlet.xml 的文件,并在那里定义相关的Beans,重写在全局中定义的任何Beans,像上面的web.xml中的代码,对应的是dispatcher-servlet.xml;当然也可以使用<init-param>元素,手动指定配置文件的路径;
dispatcher-servlet.xml 配置:
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:mvc="http://www.springframework.org/schema/mvc"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xmlns:context="http://www.springframework.org/schema/context"
7. xmlns:aop="http://www.springframework.org/schema/aop"
8. xmlns:tx="http://www.springframework.org/schema/tx"
9. xsi:schemaLocation="http://www.springframework.org/schema/beans
10. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
11. http://www.springframework.org/schema/context
12. http://www.springframework.org/schema/context/spring-context-3.0.xsd
13. http://www.springframework.org/schema/aop
14. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
15. http://www.springframework.org/schema/tx
16. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
17. http://www.springframework.org/schema/mvc
18. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
19. http://www.springframework.org/schema/context
20. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
21. <!--
22. 使Spring支持自动检测组件,如注解的Controller
23. -->
24. <context:component-scan base-package="com.minx.crm.web.controller"/>
25.
26. <bean id="viewResolver"
27. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
28. p:prefix="/WEB-INF/jsp/"
29. p:suffix=".jsp" />
30. </beans>
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
使Spring支持自动检测组件,如注解的Controller
-->
<context:component-scan base-package="com.minx.crm.web.controller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
第一个Controller:
1. package com.minx.crm.web.controller;
2.
3. import org.springframework.stereotype.Controller;
4. import org.springframework.web.bind.annotation.RequestMapping;
5. @Controller
6. public class IndexController {
7. @RequestMapping("/index")
8. public String index() {
9. return "index";
10. }
11. }
package com.minx.crm.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index")
public String index() {
return "index";
}
}
@Controller注解标识一个控制器,@RequestMapping注解标记一个访问的路径(/index.htm),return "index"标记返回视图(index.jsp);
注:如果@RequestMapping注解在类级别上,则表示一相对路径,在方法级别上,则标记访问的路径;
从@RequestMapping注解标记的访问路径中获取参数:
Spring MVC 支持RESTful风格的URL参数,如:
1. @Controller
2. public class IndexController {
3.
4. @RequestMapping("/index/{username}")
5. public String index(@PathVariable("username") String username) {
6. System.out.print(username);
7. return "index";
8. }
9. }
@Controller
public class IndexController {
@RequestMapping("/index/{username}")
public String index(@PathVariable("username") String username) {
System.out.print(username);
return "index";
}
}
在@RequestMapping中定义访问页面的URL模版,使用{}传入页面参数,使用@PathVariable 获取传入参数,即可通过地址:http://localhost:8080/crm/index/tanqimin.htm 访问;
根据不同的Web请求方法,映射到不同的处理方法:
使用登陆页面作示例,定义两个方法分辨对使用GET请求和使用POST请求访问login.htm时的响应。可以使用处理GET请求的方法显示视图,使用POST请求的方法处理业务逻辑;
1. @Controller
2. public class LoginController {
3. @RequestMapping(value = "/login", method = RequestMethod.GET)
4. public String login() {
5. return "login";
6. }
7. @RequestMapping(value = "/login", method = RequestMethod.POST)
8. public String login2(HttpServletRequest request) {
9. String username = request.getParameter("username").trim();
10. System.out.println(username);
11. return "login2";
12. }
13. }
@Controller
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login2(HttpServletRequest request) {
String username = request.getParameter("username").trim();
System.out.println(username);
return "login2";
}
}
在视图页面,通过地址栏访问login.htm,是通过GET请求访问页面,因此,返回登陆表单视图login.jsp;当在登陆表单中使用POST请求提交数据时,则访问login2方法,处理登陆业务逻辑;
防止重复提交数据,可以使用重定向视图:
1. return "redirect:/login2"
return "redirect:/login2"
可以传入方法的参数类型:
1. <strong>@RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
3. String username = request.getParameter("username");
4. System.out.println(username);
5. return null;
6. }</strong>
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String username = request.getParameter("username");
System.out.println(username);
return null;
}
可以传入HttpServletRequest、HttpServletResponse、HttpSession,值得注意的是,如果第一次访问页面,HttpSession没被创建,可能会出错;
其中,String username = request.getParameter("username");可以转换为传入的参数:
1. @RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) {
3. String username = request.getParameter("username");
4. System.out.println(username);
5. return null;
6. }
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) {
String username = request.getParameter("username");
System.out.println(username);
return null;
}
使用@RequestParam 注解获取GET请求或POST请求提交的参数;
获取Cookie的值:使用@CookieValue :
获取PrintWriter:
可以直接在Controller的方法中传入PrintWriter对象,就可以在方法中使用:
1. @RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(PrintWriter out, @RequestParam("username") String username) {
3. out.println(username);
4. return null;
5. }
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(PrintWriter out, @RequestParam("username") String username) {
out.println(username);
return null;
}
获取表单中提交的值,并封装到POJO中,传入Controller的方法里:
POJO如下(User.java):
1. public class User{
2. private long id;
3. private String username;
4. private String password;
5.
6. …此处省略 getter,setter...
7. }
public class User{
private long id;
private String username;
private String password;
…此处省略getter,setter...
}
通过表单提交,直接可以把表单值封装到User对象中:
1. @RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(PrintWriter out, User user) {
3. out.println(user.getUsername());
4. return null;
5. }
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(PrintWriter out, User user) {
out.println(user.getUsername());
return null;
}
可以把对象,put 入获取的Map对象中,传到对应的视图:
1. <strong>@RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(User user, Map model) {
3. model.put("user",user);
4. return "view";
5. }</strong>
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(User user, Map model) {
model.put("user",user);
return "view";
}
在返回的view.jsp中,就可以根据key来获取user的值(通过EL表达式,${user }即可);
Controller中方法的返回值:
void:多数用于使用PrintWriter输出响应数据;
String 类型:返回该String对应的View Name;
任意类型对象:
返回ModelAndView:
自定义视图(JstlView,ExcelView):
拦截器(Inteceptors):
1. <strong>public class MyInteceptor implements HandlerInterceptor {
2. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o)
3. throws Exception {
4. return false;
5. }
6. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav)
7. throws Exception {
8. }
9. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn)
10. throws Exception {
11. }
12. }</strong>
public class MyInteceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o)
throws Exception {
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav)
throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn)
throws Exception {
}
}
拦截器需要实现HandleInterceptor接口,并实现其三个方法:
preHandle:拦截器的前端,执行控制器之前所要处理的方法,通常用于权限控制、日志,其中,Object o表示下一个拦截器;
postHandle:控制器的方法已经执行完毕,转换成视图之前的处理;
afterCompletion:视图已处理完后执行的方法,通常用于释放资源;
在MVC的配置文件中,配置拦截器与需要拦截的URL:
1. <mvc:interceptors>
2. <mvc:interceptor>
3. <mvc:mapping path="/index.htm" />
4. <bean class="com.minx.crm.web.interceptor.MyInterceptor" />
5. </mvc:interceptor>
6. </mvc:interceptors>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/index.htm" />
<bean class="com.minx.crm.web.interceptor.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
国际化:
在MVC配置文件中,配置国际化属性文件:
1. <bean id="messageSource"
2. class="org.springframework.context.support.ResourceBundleMessageSource"
3. p:basename="message">
4. </bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="message">
</bean>
那么,Spring就会在项目中搜索相关的国际化属性文件,如:message.properties、message_zh_CN.properties
在VIEW中,引入Spring标签:<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>,使用<spring:message code="key" />调用,即可;
如果一种语言,有多个语言文件,可以更改MVC配置文件为:
1. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
2. <property name="basenames">
3. <list>
4. <value>message01</value>
5. <value>message02</value>
6. <value>message03</value>
7. </list>
8. </property>
9. </bean>
利用annotation使前后台数据交互更透明
服务端数据到客户端
1. 返回页面,Controller中方法返回String,String对应的是view的位置,如果需要携带数据通过model(相当于一个Map)传递到 view, view中使用jstl的EL表达式来绑定model带来的数据。
Java代码
1. @RequestMapping(value="/getPojoView", method=RequestMethod.GET)
2. public String getPojoView(Model model){
3. Pojo pojo = new Pojo();
4. pojo.setPojoName("testName");
5. pojo.setPojoValue("testValue");
6. model.addAttribute(pojo);
7. return"sample/pojoView";
8. }
@RequestMapping(value="/getPojoView", method=RequestMethod.GET)
public String getPojoView(Model model){
Pojo pojo = new Pojo();
pojo.setPojoName("testName");
pojo.setPojoValue("testValue");
model.addAttribute(pojo);
return"sample/pojoView";
}
2. 返回Json对象,利用@ResponseBody来实现。
Java代码
1. @RequestMapping(value="/getPojoJson", method=RequestMethod.GET)
2. public @ResponseBody Pojo getPojoJson(){
3. Pojo pojo = new Pojo();
4. pojo.setPojoName("testName");
5. pojo.setPojoValue("testValue");
6.
7. return pojo;
8. }
@RequestMapping(value="/getPojoJson", method=RequestMethod.GET)
public @ResponseBody Pojo getPojoJson(){
Pojo pojo = new Pojo();
pojo.setPojoName("testName");
pojo.setPojoValue("testValue");
return pojo;
}
注:spring mvc自动将java对象转化成了json对象传回了客户端,返回对象可以是Pojo也可以是List
3. 直接操作Response自己实现想要的效果。
Java代码
1. @RequestMapping(value="/getCustomResponse", method=RequestMethod.GET)
2. public void getCustomResponse(HttpServletResponse response){
3. //操作response...
4. }
@RequestMapping(value="/getCustomResponse", method=RequestMethod.GET)
public void getCustomResponse(HttpServletResponse response){
//操作response...
}
注:response为spring根据方法的type类型注入的
客户端数据到服务端
1. 通过URL 传回参数:
view
Html代码
1. <script type="text/javascript"src="jquery-1.4.min.js"></script>
2. <h1>button与链接效果一致</h1>
3. <a href="simple?name=text&age=28">simple</a><button onclick="simple()">simple</button><br/>
4. <script type="text/javascript">
5. function simple(){
6. $.getJSON("simple",{"name":"nameJsonTest","age":"100"},function(){});
7. }
8. </script>
9. <a href="list?names[]=aaaa&names[]=bbbb">list</a><button onclick="list()">list</button><br/>
10. <script type="text/javascript">
11. function list(){
12. $.getJSON("list",{"names":["name1","name2","name3"]},function(){});
13. }
14. </script>
15. <a href="pojo?pojo[pojoName]=hahaha&pojo[pojoValue]=kkkkkk">pojo</a><button onclick="pojo()">pojo</button><br/>
16. <script type="text/javascript">
17. function pojo(){
18. $.getJSON("pojo",{"pojo":{"pojoName":"testName","pojoValue":"testValue"}},function(){});
19. }
20. </script>
21. <a href="rest/10/2">rest</a><button onclick="rest()">rest</button><br/>
22. <script type="text/javascript">
23. function rest(){
24. var pageSize = 20;
25. var pageNo = 3;
26. $.getJSON("rest/"+pageSize+"/"+pageNo,{},function(){});
27. }
28. </script>
<script type="text/javascript"src="jquery-1.4.min.js"></script>
<h1>button与链接效果一致</h1>
<a href="simple?name=text&age=28">simple</a><button onclick="simple()">simple</button><br/>
<script type="text/javascript">
function simple(){
$.getJSON("simple",{"name":"nameJsonTest","age":"100"},function(){});
}
</script>
<a href="list?names[]=aaaa&names[]=bbbb">list</a><button onclick="list()">list</button><br/>
<script type="text/javascript">
function list(){
$.getJSON("list",{"names":["name1","name2","name3"]},function(){});
}
</script>
<a href="pojo?pojo[pojoName]=hahaha&pojo[pojoValue]=kkkkkk">pojo</a><button onclick="pojo()">pojo</button><br/>
<script type="text/javascript">
function pojo(){
$.getJSON("pojo",{"pojo":{"pojoName":"testName","pojoValue":"testValue"}},function(){});
}
</script>
<a href="rest/10/2">rest</a><button onclick="rest()">rest</button><br/>
<script type="text/javascript">
function rest(){
var pageSize = 20;
var pageNo = 3;
$.getJSON("rest/"+pageSize+"/"+pageNo,{},function(){});
}
</script>
controller
Java代码
1. package sample;
2.
3. import org.springframework.stereotype.Controller;
4. import org.springframework.web.bind.annotation.PathVariable;
5. import org.springframework.web.bind.annotation.RequestMapping;
6. import org.springframework.web.bind.annotation.RequestMethod;
7. import org.springframework.web.bind.annotation.RequestParam;
8.
9. @Controller
10. @RequestMapping(value="/urlparam")
11. public class UrlParamController {
12. @RequestMapping(value="/", method=RequestMethod.GET)
13. public String index(){
14. return"urlparam/index";
15. }
16.
17. @RequestMapping(value="/simple", method=RequestMethod.GET)
18. public void simple(@RequestParam String name, @RequestParam Integer age){
19. System.out.println("name:"+name);
20. System.out.println("age:"+age);
21. }
22.
23. //list内不能放POJO对象
24. @RequestMapping(value="/list", method=RequestMethod.GET)
25. public void list(@RequestParam("names[]") String[] names){
26. //也可以用List<String> names来接收
27. for(String name : names){
28. System.out.println("name:"+name);
29. }
30. }
31.
32. //单URL目前还不支持POJO对象,只能支持键值对,希望spring以后有所改善
33. @RequestMapping(value="/pojo", method=RequestMethod.GET)
34. public void pojo(@RequestParam("pojo[pojoName]") String name, @RequestParam("pojo[pojoValue]") String value){
35. System.out.println("name:"+name);
36. System.out.println("value:"+value);
37. }
38.
39. @RequestMapping(value="/rest/{pageSize}/{pageNo}", method=RequestMethod.GET)
40. public void rest(@PathVariable Integer pageSize, @PathVariable Integer pageNo){
41. System.out.println("pageSize:"+pageSize);
42. System.out.println("pageNo:"+pageNo);
43. }
44. }
package sample;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(value="/urlparam")
public class UrlParamController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String index(){
return"urlparam/index";
}
@RequestMapping(value="/simple", method=RequestMethod.GET)
public void simple(@RequestParam String name, @RequestParam Integer age){
System.out.println("name:"+name);
System.out.println("age:"+age);
}
//list内不能放POJO对象
@RequestMapping(value="/list", method=RequestMethod.GET)
public void list(@RequestParam("names[]") String[] names){
//也可以用List<String> names来接收
for(String name : names){
System.out.println("name:"+name);
}
}
//单URL目前还不支持POJO对象,只能支持键值对,希望spring以后有所改善
@RequestMapping(value="/pojo", method=RequestMethod.GET)
public void pojo(@RequestParam("pojo[pojoName]") String name, @RequestParam("pojo[pojoValue]") String value){
System.out.println("name:"+name);
System.out.println("value:"+value);
}
@RequestMapping(value="/rest/{pageSize}/{pageNo}", method=RequestMethod.GET)
public void rest(@PathVariable Integer pageSize, @PathVariable Integer pageNo){
System.out.println("pageSize:"+pageSize);
System.out.println("pageNo:"+pageNo);
}
}
2. 通过POST表单传回参数:
方式同与url的是一致的,需要将method=RequestMethod.POST,不过有中文的话一般都用post来避免转码。一般ajax的时候用$.post而不能使用jQuery插件json的$.postJSON。下面会讲到。
3. 通过使用jQuery插件json的$.postJSON传回参数:
$.postJSON返回的是:application/json,
$.post 返回的是: application/x-www-form-urlencoded
spring 会将postJSON传回的json字符串转换成对象再将对象丢给带有@RequestBody的形参。 由于json字符串直接转换为对象,所以@RequestBody只能接收一个对象还需要属性一一对应,不能多传参数。此方式可以传POJO,也可以传List<POJO>。
Js代码
1. $.postJSON('url', {"name":"testName","age":"28"},function(){});
$.postJSON('url', {"name":"testName","age":"28"},function(){});
Java代码
1. <span style="font-family: Arial,Arial,Helvetica,sans-serif; font-size: 18px; white-space: normal;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 12px; white-space: pre;">class Pojo{
2. </span></span> private Long id;
3. private String name;
4. private Integer age;
5. //get,set...
6. }
class Pojo{
private Long id;
private String name;
private Integer age;
//get,set...
}
Java代码
1. @RequestMapping(value="pojo", method=RequestMethod.POST)
2. publicvoid sentPojo(@RequestBody Pojo pojo){
3. System.out.println(pojo.getPojoName());
4. System.out.println(pojo.getPojoValue());
5. }
@RequestMapping(value="pojo", method=RequestMethod.POST)
publicvoid sentPojo(@RequestBody Pojo pojo){
System.out.println(pojo.getPojoName());
System.out.println(pojo.getPojoValue());
}
注:目前对于一对象,附带几个简单参数的解决办法是将简单参数通过为REST的 url路径参数来传送。
4. 直接拿到Request来操作:
Java代码
1. @RequestMapping(value="/", method=RequestMethod.GET)
2. public String withRequest(HttpServletRequest request){
3. //操作request...
4. return"someview";
5. }
@RequestMapping(value="/", method=RequestMethod.GET)
public String withRequest(HttpServletRequest request){
//操作request...
return"someview";
}
以上controller内的方法的形参, 除了@RequestBody和@RequestParam不能同时声明外,都可以进行组合来满足各种需求。
小结:spring mvc3充分利用了annotation的好处将参数传递过程中的转换全部变为了透明,这样省去了程序员对参数进行一些无聊的转换,这肯定能提高不少效率。另一方面想说的是spring的@RequestBody还可以做的更好,如果能允许多个对象同时传送,那这个东西就十分好了。
1. <servlet>
2. <servlet-name>dispatcher</servlet-name>
3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4. <init-param>
5. <description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC 的配置文件</description>
6. <param-name>contextConfigLocation</param-name>
7. <param-value>/WEB-INF/spring-mvc/*.xml</param-value>
8. </init-param>
9. <load-on-startup>1</load-on-startup>
10. </servlet>
11. <servlet-mapping>
12. <servlet-name>dispatcher</servlet-name>
13. <url-pattern>*.htm</url-pattern>
14. </servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>加载/WEB-INF/spring-mvc/目录下的所有XML作为Spring MVC的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
这样,所有的.htm的请求,都会被DispatcherServlet处理;
初始化 DispatcherServlet 时,该框架在 web 应用程序WEB-INF 目录中寻找一个名为[servlet-名称]-servlet.xml 的文件,并在那里定义相关的Beans,重写在全局中定义的任何Beans,像上面的web.xml中的代码,对应的是dispatcher-servlet.xml;当然也可以使用<init-param>元素,手动指定配置文件的路径;
dispatcher-servlet.xml 配置:
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:mvc="http://www.springframework.org/schema/mvc"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xmlns:context="http://www.springframework.org/schema/context"
7. xmlns:aop="http://www.springframework.org/schema/aop"
8. xmlns:tx="http://www.springframework.org/schema/tx"
9. xsi:schemaLocation="http://www.springframework.org/schema/beans
10. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
11. http://www.springframework.org/schema/context
12. http://www.springframework.org/schema/context/spring-context-3.0.xsd
13. http://www.springframework.org/schema/aop
14. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
15. http://www.springframework.org/schema/tx
16. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
17. http://www.springframework.org/schema/mvc
18. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
19. http://www.springframework.org/schema/context
20. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
21. <!--
22. 使Spring支持自动检测组件,如注解的Controller
23. -->
24. <context:component-scan base-package="com.minx.crm.web.controller"/>
25.
26. <bean id="viewResolver"
27. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
28. p:prefix="/WEB-INF/jsp/"
29. p:suffix=".jsp" />
30. </beans>
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
使Spring支持自动检测组件,如注解的Controller
-->
<context:component-scan base-package="com.minx.crm.web.controller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
第一个Controller:
1. package com.minx.crm.web.controller;
2.
3. import org.springframework.stereotype.Controller;
4. import org.springframework.web.bind.annotation.RequestMapping;
5. @Controller
6. public class IndexController {
7. @RequestMapping("/index")
8. public String index() {
9. return "index";
10. }
11. }
package com.minx.crm.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping("/index")
public String index() {
return "index";
}
}
@Controller注解标识一个控制器,@RequestMapping注解标记一个访问的路径(/index.htm),return "index"标记返回视图(index.jsp);
注:如果@RequestMapping注解在类级别上,则表示一相对路径,在方法级别上,则标记访问的路径;
从@RequestMapping注解标记的访问路径中获取参数:
Spring MVC 支持RESTful风格的URL参数,如:
1. @Controller
2. public class IndexController {
3.
4. @RequestMapping("/index/{username}")
5. public String index(@PathVariable("username") String username) {
6. System.out.print(username);
7. return "index";
8. }
9. }
@Controller
public class IndexController {
@RequestMapping("/index/{username}")
public String index(@PathVariable("username") String username) {
System.out.print(username);
return "index";
}
}
在@RequestMapping中定义访问页面的URL模版,使用{}传入页面参数,使用@PathVariable 获取传入参数,即可通过地址:http://localhost:8080/crm/index/tanqimin.htm 访问;
根据不同的Web请求方法,映射到不同的处理方法:
使用登陆页面作示例,定义两个方法分辨对使用GET请求和使用POST请求访问login.htm时的响应。可以使用处理GET请求的方法显示视图,使用POST请求的方法处理业务逻辑;
1. @Controller
2. public class LoginController {
3. @RequestMapping(value = "/login", method = RequestMethod.GET)
4. public String login() {
5. return "login";
6. }
7. @RequestMapping(value = "/login", method = RequestMethod.POST)
8. public String login2(HttpServletRequest request) {
9. String username = request.getParameter("username").trim();
10. System.out.println(username);
11. return "login2";
12. }
13. }
@Controller
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login2(HttpServletRequest request) {
String username = request.getParameter("username").trim();
System.out.println(username);
return "login2";
}
}
在视图页面,通过地址栏访问login.htm,是通过GET请求访问页面,因此,返回登陆表单视图login.jsp;当在登陆表单中使用POST请求提交数据时,则访问login2方法,处理登陆业务逻辑;
防止重复提交数据,可以使用重定向视图:
1. return "redirect:/login2"
return "redirect:/login2"
可以传入方法的参数类型:
1. <strong>@RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
3. String username = request.getParameter("username");
4. System.out.println(username);
5. return null;
6. }</strong>
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
String username = request.getParameter("username");
System.out.println(username);
return null;
}
可以传入HttpServletRequest、HttpServletResponse、HttpSession,值得注意的是,如果第一次访问页面,HttpSession没被创建,可能会出错;
其中,String username = request.getParameter("username");可以转换为传入的参数:
1. @RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) {
3. String username = request.getParameter("username");
4. System.out.println(username);
5. return null;
6. }
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam("username") String username) {
String username = request.getParameter("username");
System.out.println(username);
return null;
}
使用@RequestParam 注解获取GET请求或POST请求提交的参数;
获取Cookie的值:使用@CookieValue :
获取PrintWriter:
可以直接在Controller的方法中传入PrintWriter对象,就可以在方法中使用:
1. @RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(PrintWriter out, @RequestParam("username") String username) {
3. out.println(username);
4. return null;
5. }
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(PrintWriter out, @RequestParam("username") String username) {
out.println(username);
return null;
}
获取表单中提交的值,并封装到POJO中,传入Controller的方法里:
POJO如下(User.java):
1. public class User{
2. private long id;
3. private String username;
4. private String password;
5.
6. …此处省略 getter,setter...
7. }
public class User{
private long id;
private String username;
private String password;
…此处省略getter,setter...
}
通过表单提交,直接可以把表单值封装到User对象中:
1. @RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(PrintWriter out, User user) {
3. out.println(user.getUsername());
4. return null;
5. }
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(PrintWriter out, User user) {
out.println(user.getUsername());
return null;
}
可以把对象,put 入获取的Map对象中,传到对应的视图:
1. <strong>@RequestMapping(value = "login", method = RequestMethod.POST)
2. public String testParam(User user, Map model) {
3. model.put("user",user);
4. return "view";
5. }</strong>
@RequestMapping(value = "login", method = RequestMethod.POST)
public String testParam(User user, Map model) {
model.put("user",user);
return "view";
}
在返回的view.jsp中,就可以根据key来获取user的值(通过EL表达式,${user }即可);
Controller中方法的返回值:
void:多数用于使用PrintWriter输出响应数据;
String 类型:返回该String对应的View Name;
任意类型对象:
返回ModelAndView:
自定义视图(JstlView,ExcelView):
拦截器(Inteceptors):
1. <strong>public class MyInteceptor implements HandlerInterceptor {
2. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o)
3. throws Exception {
4. return false;
5. }
6. public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav)
7. throws Exception {
8. }
9. public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn)
10. throws Exception {
11. }
12. }</strong>
public class MyInteceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o)
throws Exception {
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav)
throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn)
throws Exception {
}
}
拦截器需要实现HandleInterceptor接口,并实现其三个方法:
preHandle:拦截器的前端,执行控制器之前所要处理的方法,通常用于权限控制、日志,其中,Object o表示下一个拦截器;
postHandle:控制器的方法已经执行完毕,转换成视图之前的处理;
afterCompletion:视图已处理完后执行的方法,通常用于释放资源;
在MVC的配置文件中,配置拦截器与需要拦截的URL:
1. <mvc:interceptors>
2. <mvc:interceptor>
3. <mvc:mapping path="/index.htm" />
4. <bean class="com.minx.crm.web.interceptor.MyInterceptor" />
5. </mvc:interceptor>
6. </mvc:interceptors>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/index.htm" />
<bean class="com.minx.crm.web.interceptor.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
国际化:
在MVC配置文件中,配置国际化属性文件:
1. <bean id="messageSource"
2. class="org.springframework.context.support.ResourceBundleMessageSource"
3. p:basename="message">
4. </bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="message">
</bean>
那么,Spring就会在项目中搜索相关的国际化属性文件,如:message.properties、message_zh_CN.properties
在VIEW中,引入Spring标签:<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>,使用<spring:message code="key" />调用,即可;
如果一种语言,有多个语言文件,可以更改MVC配置文件为:
1. <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
2. <property name="basenames">
3. <list>
4. <value>message01</value>
5. <value>message02</value>
6. <value>message03</value>
7. </list>
8. </property>
9. </bean>
利用annotation使前后台数据交互更透明
服务端数据到客户端
1. 返回页面,Controller中方法返回String,String对应的是view的位置,如果需要携带数据通过model(相当于一个Map)传递到 view, view中使用jstl的EL表达式来绑定model带来的数据。
Java代码
1. @RequestMapping(value="/getPojoView", method=RequestMethod.GET)
2. public String getPojoView(Model model){
3. Pojo pojo = new Pojo();
4. pojo.setPojoName("testName");
5. pojo.setPojoValue("testValue");
6. model.addAttribute(pojo);
7. return"sample/pojoView";
8. }
@RequestMapping(value="/getPojoView", method=RequestMethod.GET)
public String getPojoView(Model model){
Pojo pojo = new Pojo();
pojo.setPojoName("testName");
pojo.setPojoValue("testValue");
model.addAttribute(pojo);
return"sample/pojoView";
}
2. 返回Json对象,利用@ResponseBody来实现。
Java代码
1. @RequestMapping(value="/getPojoJson", method=RequestMethod.GET)
2. public @ResponseBody Pojo getPojoJson(){
3. Pojo pojo = new Pojo();
4. pojo.setPojoName("testName");
5. pojo.setPojoValue("testValue");
6.
7. return pojo;
8. }
@RequestMapping(value="/getPojoJson", method=RequestMethod.GET)
public @ResponseBody Pojo getPojoJson(){
Pojo pojo = new Pojo();
pojo.setPojoName("testName");
pojo.setPojoValue("testValue");
return pojo;
}
注:spring mvc自动将java对象转化成了json对象传回了客户端,返回对象可以是Pojo也可以是List
3. 直接操作Response自己实现想要的效果。
Java代码
1. @RequestMapping(value="/getCustomResponse", method=RequestMethod.GET)
2. public void getCustomResponse(HttpServletResponse response){
3. //操作response...
4. }
@RequestMapping(value="/getCustomResponse", method=RequestMethod.GET)
public void getCustomResponse(HttpServletResponse response){
//操作response...
}
注:response为spring根据方法的type类型注入的
客户端数据到服务端
1. 通过URL 传回参数:
view
Html代码
1. <script type="text/javascript"src="jquery-1.4.min.js"></script>
2. <h1>button与链接效果一致</h1>
3. <a href="simple?name=text&age=28">simple</a><button onclick="simple()">simple</button><br/>
4. <script type="text/javascript">
5. function simple(){
6. $.getJSON("simple",{"name":"nameJsonTest","age":"100"},function(){});
7. }
8. </script>
9. <a href="list?names[]=aaaa&names[]=bbbb">list</a><button onclick="list()">list</button><br/>
10. <script type="text/javascript">
11. function list(){
12. $.getJSON("list",{"names":["name1","name2","name3"]},function(){});
13. }
14. </script>
15. <a href="pojo?pojo[pojoName]=hahaha&pojo[pojoValue]=kkkkkk">pojo</a><button onclick="pojo()">pojo</button><br/>
16. <script type="text/javascript">
17. function pojo(){
18. $.getJSON("pojo",{"pojo":{"pojoName":"testName","pojoValue":"testValue"}},function(){});
19. }
20. </script>
21. <a href="rest/10/2">rest</a><button onclick="rest()">rest</button><br/>
22. <script type="text/javascript">
23. function rest(){
24. var pageSize = 20;
25. var pageNo = 3;
26. $.getJSON("rest/"+pageSize+"/"+pageNo,{},function(){});
27. }
28. </script>
<script type="text/javascript"src="jquery-1.4.min.js"></script>
<h1>button与链接效果一致</h1>
<a href="simple?name=text&age=28">simple</a><button onclick="simple()">simple</button><br/>
<script type="text/javascript">
function simple(){
$.getJSON("simple",{"name":"nameJsonTest","age":"100"},function(){});
}
</script>
<a href="list?names[]=aaaa&names[]=bbbb">list</a><button onclick="list()">list</button><br/>
<script type="text/javascript">
function list(){
$.getJSON("list",{"names":["name1","name2","name3"]},function(){});
}
</script>
<a href="pojo?pojo[pojoName]=hahaha&pojo[pojoValue]=kkkkkk">pojo</a><button onclick="pojo()">pojo</button><br/>
<script type="text/javascript">
function pojo(){
$.getJSON("pojo",{"pojo":{"pojoName":"testName","pojoValue":"testValue"}},function(){});
}
</script>
<a href="rest/10/2">rest</a><button onclick="rest()">rest</button><br/>
<script type="text/javascript">
function rest(){
var pageSize = 20;
var pageNo = 3;
$.getJSON("rest/"+pageSize+"/"+pageNo,{},function(){});
}
</script>
controller
Java代码
1. package sample;
2.
3. import org.springframework.stereotype.Controller;
4. import org.springframework.web.bind.annotation.PathVariable;
5. import org.springframework.web.bind.annotation.RequestMapping;
6. import org.springframework.web.bind.annotation.RequestMethod;
7. import org.springframework.web.bind.annotation.RequestParam;
8.
9. @Controller
10. @RequestMapping(value="/urlparam")
11. public class UrlParamController {
12. @RequestMapping(value="/", method=RequestMethod.GET)
13. public String index(){
14. return"urlparam/index";
15. }
16.
17. @RequestMapping(value="/simple", method=RequestMethod.GET)
18. public void simple(@RequestParam String name, @RequestParam Integer age){
19. System.out.println("name:"+name);
20. System.out.println("age:"+age);
21. }
22.
23. //list内不能放POJO对象
24. @RequestMapping(value="/list", method=RequestMethod.GET)
25. public void list(@RequestParam("names[]") String[] names){
26. //也可以用List<String> names来接收
27. for(String name : names){
28. System.out.println("name:"+name);
29. }
30. }
31.
32. //单URL目前还不支持POJO对象,只能支持键值对,希望spring以后有所改善
33. @RequestMapping(value="/pojo", method=RequestMethod.GET)
34. public void pojo(@RequestParam("pojo[pojoName]") String name, @RequestParam("pojo[pojoValue]") String value){
35. System.out.println("name:"+name);
36. System.out.println("value:"+value);
37. }
38.
39. @RequestMapping(value="/rest/{pageSize}/{pageNo}", method=RequestMethod.GET)
40. public void rest(@PathVariable Integer pageSize, @PathVariable Integer pageNo){
41. System.out.println("pageSize:"+pageSize);
42. System.out.println("pageNo:"+pageNo);
43. }
44. }
package sample;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(value="/urlparam")
public class UrlParamController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String index(){
return"urlparam/index";
}
@RequestMapping(value="/simple", method=RequestMethod.GET)
public void simple(@RequestParam String name, @RequestParam Integer age){
System.out.println("name:"+name);
System.out.println("age:"+age);
}
//list内不能放POJO对象
@RequestMapping(value="/list", method=RequestMethod.GET)
public void list(@RequestParam("names[]") String[] names){
//也可以用List<String> names来接收
for(String name : names){
System.out.println("name:"+name);
}
}
//单URL目前还不支持POJO对象,只能支持键值对,希望spring以后有所改善
@RequestMapping(value="/pojo", method=RequestMethod.GET)
public void pojo(@RequestParam("pojo[pojoName]") String name, @RequestParam("pojo[pojoValue]") String value){
System.out.println("name:"+name);
System.out.println("value:"+value);
}
@RequestMapping(value="/rest/{pageSize}/{pageNo}", method=RequestMethod.GET)
public void rest(@PathVariable Integer pageSize, @PathVariable Integer pageNo){
System.out.println("pageSize:"+pageSize);
System.out.println("pageNo:"+pageNo);
}
}
2. 通过POST表单传回参数:
方式同与url的是一致的,需要将method=RequestMethod.POST,不过有中文的话一般都用post来避免转码。一般ajax的时候用$.post而不能使用jQuery插件json的$.postJSON。下面会讲到。
3. 通过使用jQuery插件json的$.postJSON传回参数:
$.postJSON返回的是:application/json,
$.post 返回的是: application/x-www-form-urlencoded
spring 会将postJSON传回的json字符串转换成对象再将对象丢给带有@RequestBody的形参。 由于json字符串直接转换为对象,所以@RequestBody只能接收一个对象还需要属性一一对应,不能多传参数。此方式可以传POJO,也可以传List<POJO>。
Js代码
1. $.postJSON('url', {"name":"testName","age":"28"},function(){});
$.postJSON('url', {"name":"testName","age":"28"},function(){});
Java代码
1. <span style="font-family: Arial,Arial,Helvetica,sans-serif; font-size: 18px; white-space: normal;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 12px; white-space: pre;">class Pojo{
2. </span></span> private Long id;
3. private String name;
4. private Integer age;
5. //get,set...
6. }
class Pojo{
private Long id;
private String name;
private Integer age;
//get,set...
}
Java代码
1. @RequestMapping(value="pojo", method=RequestMethod.POST)
2. publicvoid sentPojo(@RequestBody Pojo pojo){
3. System.out.println(pojo.getPojoName());
4. System.out.println(pojo.getPojoValue());
5. }
@RequestMapping(value="pojo", method=RequestMethod.POST)
publicvoid sentPojo(@RequestBody Pojo pojo){
System.out.println(pojo.getPojoName());
System.out.println(pojo.getPojoValue());
}
注:目前对于一对象,附带几个简单参数的解决办法是将简单参数通过为REST的 url路径参数来传送。
4. 直接拿到Request来操作:
Java代码
1. @RequestMapping(value="/", method=RequestMethod.GET)
2. public String withRequest(HttpServletRequest request){
3. //操作request...
4. return"someview";
5. }
@RequestMapping(value="/", method=RequestMethod.GET)
public String withRequest(HttpServletRequest request){
//操作request...
return"someview";
}
以上controller内的方法的形参, 除了@RequestBody和@RequestParam不能同时声明外,都可以进行组合来满足各种需求。
小结:spring mvc3充分利用了annotation的好处将参数传递过程中的转换全部变为了透明,这样省去了程序员对参数进行一些无聊的转换,这肯定能提高不少效率。另一方面想说的是spring的@RequestBody还可以做的更好,如果能允许多个对象同时传送,那这个东西就十分好了。
发表评论
-
关于Java生成背景透明的png图片
2010-12-29 15:28 3415最近用到Java动态生成背景透明的图片功能,从gi ... -
JSTL标签用法:<c:choose><c:forEach><c:if><c:when><c:set>
2010-12-15 15:47 1446JSTL标签用法 关键字:J ... -
HttpSession用法
2010-12-15 15:46 3314在HttpServlet中,HttpSession对象通常在r ... -
创建session学习-request.getSession()
2010-12-09 16:23 946在HttpServlet中,HttpSession对象通常在r ... -
创建session学习-request.getSession()
2010-12-09 15:35 980在HttpServlet中,HttpSession对象通常在r ... -
Spring MVC的@ResponseBody返回JSON串
2010-12-07 16:01 3333Xml代码 <bean class ="or ...
相关推荐
Spring MVC 入门级资料 Spring MVC 是一个基于 Java 的 Web 框架,它提供了一种灵活、可扩展、Easy-to-use 的 Web 应用程序开发方式。Spring MVC 是 Spring 框架的一部分,提供了一个强大、灵活、可扩展的 MVC 框架...
这个经典入门案例将引导你逐步了解并掌握 Spring MVC 的基本概念、配置以及实际应用。 1. **Spring MVC 概述** Spring MVC 是一个模型-视图-控制器(MVC)架构模式的实现,它简化了Java Web 应用的开发,提供了一...
这个“spring MVC_快速入门”文档可能涵盖了以上部分或全部内容,旨在帮助初学者快速理解Spring MVC的工作原理和基本用法。通过学习,开发者可以快速搭建起一个功能完善的Web应用,并逐步深入到更高级的特性,如AOP...
在本文中,我们将深入探讨基于注解的Spring MVC的简单入门。Spring MVC是Spring框架的一个模块,主要用于构建Web应用程序,提供了强大的MVC(Model-View-Controller)设计模式支持。通过注解,开发者可以简化配置,...
首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController ...
这个"精简源码-spring-mvc 入门级使用"的压缩包文件,旨在为初学者提供一个简单易懂的实践案例,帮助他们快速理解和上手 Spring MVC。 首先,我们从 "spring" 这个标签开始。Spring 是一个开源框架,它提供了一个...
Spring MVC简单易用,可以快速提高开发效率,且性能优秀,社区活跃,文档丰富。由于支持注解配置,使得框架更加易用。相较于Struts2,Spring MVC避免了一些可能导致性能下降的特性,如值栈、OGNL表达式等。 二、...
在这个入门教程中,我们将通过一个简单的登录示例来学习 Spring MVC 的基本概念和工作流程。 ### 1. Spring MVC 架构 Spring MVC 包含了以下组件: - **DispatcherServlet**:它是整个流程的入口,负责接收请求并...
个人认为相当适合入门和知识巩固!! 一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 六、springMVC-mvc.xml 配置文件...
【Spring MVC 快速入门】 Spring MVC 是一个强大的基于Java的Web应用开发框架,它提供了模型-视图-控制器(MVC)架构模式,使得开发者能够有效地分离业务逻辑和表现层。Spring MVC 以其简洁、高效和高度可扩展性...
Spring MVC 是一款基于 Java 的轻量级 Web 开发框架,它是 Spring 框架的重要组成部分,主要用于构建 MVC(Model-View-Controller)架构的 Web 应用程序。本指南将深入探讨 Spring MVC 的核心概念、配置、控制器、...
Spring MVC 教程 快速入门 深入分析 目录 一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 六、springMVC-mvc.xml 配置...
本教程将带你入门 Spring MVC,理解其核心概念和工作原理,以及如何在实际项目中应用。 一、Spring MVC 架构 Spring MVC 提供了一个模型-视图-控制器的设计模式,它将应用程序逻辑、用户界面和数据处理分离,使得...
Spring MVC 是一个强大的Java Web开发框架,用于构建可维护、高性能和灵活的Web应用程序。它作为Spring框架的一部分,提供了一种模型-视图-控制器(MVC)架构,帮助开发者将业务逻辑、用户界面和数据访问分离开来,...
### Spring MVC 快速入门深入分析 #### 一、Spring MVC 概述 Spring MVC 是 Spring Framework 的一个重要模块,主要用于构建 Web 应用程序。它遵循 MVC(Model-View-Controller)设计模式,将应用程序的不同关注点...
Spring Web MVC是一种基于MVC模式的轻量级Java Web应用框架,它是Spring框架的一部分,主要用于简化Web层的开发。Spring Web MVC允许开发者将应用程序分为三个主要组件:模型(Model)、视图(View)和控制器(Controller...
本教程将深入探讨Spring MVC的基本概念和关键组件,帮助初学者快速入门。 首先,让我们理解Spring MVC的核心组件: 1. **DispatcherServlet**:它是Spring MVC的前端控制器,负责接收HTTP请求,根据请求的URL和...
**Spring MVC 框架详解** Spring MVC 是 Spring 框架的重要组成部分,是一个轻量级的、模型-视图-控制器(Model-View-Controller)的 Web 应用开发框架,它允许开发者以简洁、高效的方式构建动态的、数据驱动的 Web...