spring mvc 是当前java比较流行的一种mvc架构,下文主要展示spring mvc 使用的配置。
web.xml 的统一配置(仅展示和spring 配置相关部分)
1、完全基于注解的方案。
servlet-context.xml 配置:
controller 注解
以上ok
http://localhost:8080/login/guest
2、基于xml的配置
servlet-context.xml 配置:
java代码:
请求:
http://localhost:8080/login/guest
http://localhost:8080/login/guest2
http://localhost:8080/login/guest3
ok~
web.xml 的统一配置(仅展示和spring 配置相关部分)
<!-- Spring context 服务层的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/*Context.xml,
classpath*:META-INF/spring/*-beans.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- app servlet -->
<servlet>
<servlet-name>AppServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/ServletConfigs/AppServlet/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 覆盖default servlet的/, springmvc servlet将处理原来处理静态资源的映射 -->
<servlet-mapping>
<servlet-name>AppServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
1、完全基于注解的方案。
servlet-context.xml 配置:
<!-- 自动搜索@Controller标注的类 -->
<context:component-scan base-package="org.login.web" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 默认的注解映射的支持
<mvc:annotation-driven />
-->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
controller 注解
@Controller
@RequestMapping("/login")
public class LoginController {
private final static Logger logger = LoggerFactory.getLogger(LoginController.class);
@RequestMapping("/guest")
public String login(HttpServletRequest request,HttpServletResponse response){
logger.info("hello {}",request);
request.setAttribute("username", "admin");
request.setAttribute("password", "123456");
return "login";
}
}
以上ok
http://localhost:8080/login/guest
2、基于xml的配置
servlet-context.xml 配置:
<bean id = "loginHandleRequest" class="org.login.web.LoginHandleRequest"/>
<bean id = "loginController" class="org.login.web.LoginController"/>
<bean id = "loginServlet" class="org.login.web.LoginServlet"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/login/guest" value="loginHandleRequest"/>
<entry key="/login/guest2" value="loginController"/>
<entry key="/login/guest3" value="loginServlet"/>
</map>
</property>
</bean>
<bean class = "org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
<bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class = "org.springframework.web.servlet.handler.SimpleServletHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
java代码:
/**
* @author wangxinchun
*/
public class LoginController implements Controller {
private final static Logger logger = LoggerFactory.getLogger(LoginController.class);
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
logger.info("hello {}", request);
request.setAttribute("username", "admin");
request.setAttribute("password", "123456");
return new ModelAndView("login");
}
}
/**
* @author wangxinchun
*/
public class LoginHandleRequest implements HttpRequestHandler {
private final static Logger logger = LoggerFactory.getLogger(LoginHandleRequest.class);
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("hello {}", request);
request.setAttribute("username", "admin");
request.setAttribute("password", "123456");
request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
}
}
/**
* @author wangxinchun
*/
public class LoginServlet extends GenericServlet {
private static final long serialVersionUID = 1L;
private final static Logger logger = LoggerFactory.getLogger(LoginServlet.class);
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
logger.info("hello {}", request);
request.setAttribute("username", "admin");
request.setAttribute("password", "123456");
request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);
}
}
请求:
http://localhost:8080/login/guest
http://localhost:8080/login/guest2
http://localhost:8080/login/guest3
ok~