spring mvc现在如火如荼,闲来无事也摆弄一番。
总体来说,spring mvc还是挺不错的,注解方式的配置,RESTful的风格,让人眼睛一亮,还有不错的性能。
废话不说,直接写上我的配置吧!
要用spring mvc 需要下载spring的包,还有freemarker。
web.xml配置
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
spring mvc 主要通过DispatcherServlet做分发。
上面的servlet名字叫springmvc 所以在WEB-INF下面新建一个springmvc-servlet.xml文件
springmvc-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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"> <!--注解说明 --> <context:annotation-config /> <!-- 把标记了@Controller注解的类转换为bean --> <context:component-scan base-package="com.lantii.action" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 请求映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- freemarker的配置 --> <bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/view/" /> <property name="defaultEncoding" value="GBK" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">10</prop> <prop key="locale">zh_CN</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean> <!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <property name="suffix" value=".ftl" /> <property name="contentType" value="text/html;charset=GBK" /> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="exposeSpringMacroHelpers" value="true" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages" /> </beans>
配置完成,我们来写控制层代码:
MessageController.java
package com.lantii.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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; @Controller @RequestMapping("/message") // url映射的名称 public class MessageController { @RequestMapping(value = "/{msg}", method = RequestMethod.GET) public String showMessage(@PathVariable String msg, HttpServletRequest request, HttpServletResponse response) { // TODO 处理请求 request.setAttribute("message", msg); // 设置返回消息 return "message"; // 设置返回页面,这里对应 /WEB-INF/view 目录下的 message.ftl 文件 } @RequestMapping(value = "/add") public String addMessage(HttpServletRequest request, HttpServletResponse response) { request.setAttribute("message", "message is added"); return "message"; } }
UserController
package com.lantii.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.lantii.business.user.User; @Controller @RequestMapping("/user") public class UserController { @RequestMapping(value = "/login") public String test(HttpServletRequest request, HttpServletResponse response, User userinfo) { // 非常方便可以直接在方法里面放入对象 if (userinfo.getUsername().equals("lantii") && userinfo.getPassword().equals("ok")) { request.setAttribute("userinfo", userinfo); return "users/loginok"; // 判断,将跳转不同的页面 } else { return "users/loginerr"; // 判断,将跳转不同的页面 } } }
User.java
package com.lantii.business.user; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
返回页message.ftl
<html> <body> <p>This is my message:<br> ${message}</p> </body> </html>
users下loginok.ftl
<html> <body> hi, <b>${userinfo.username}</b> </body> </html>
接下来写一个前台调用的测试jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!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=GBK"> <title>主页</title> </head> <body> <form METHOD=POST ACTION="user/login"> 用户名:<input TYPE="text" NAME="username" value="lantii"><br> 密 码:<input TYPE="text" NAME="password" value="ok"><br> <input TYPE="submit"> </form> <br> <a href="message/add" target="_blank">add</a> <br> <a href="message/哈哈哈" target="_blank">see:哈哈哈</a> </body> </html>
接下来就可用了。