Config Spring MVC
2016-09-14 本文已影响39人
ibyr
1. Config web.xml, so that web container can start Spring container auto.
<!-- load Spring config file of applicationContext.xml, keyword classpath means loading under class path. -->
<context-param>
<param-name> contextConfigLocation </param-name>
<param-value> classpath:applicationContext.xml </param-value>
</context-param>
<!-- listener to start Spring container. -->
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<!-- servlet of Spring MVC -->
<servlet>
<servlet-name> viewspace </servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
<load-on-startup> 2 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name> viewspace </servlet-name>
<!-- All the URL ending with .html will be captured by viewspace servlet and send to Spring MVC. -->
<servlet-pattern> *.html </servlet-pattern>
</servlet-mapping>
2.Create config file of Spring MVC: viewspace-servlet.xml.
According with convention, the config file is named as the rule: <servlet-name>-servlet.xml.
And servlet of Spring MVC will auto combine <servlet-name>-servlet.xml with other config file of Spring.
<!-- scan controller and apply @Controller -->
<context:component-scan base-package="com.huang.web"/>
<!-- config view parser, shift ModelAndView and string into concrete pages. -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewclass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
3. Config your controller.
@Controller + <context:component-scan> = A controller, which makes a POJO own the ability of handling HTTP request.
@Controller // To declare it's a controller.
@RequestMapping(value="/admin") // root directory is: /admin.
public class LoginController {
@Autowired
private UserService userService;
/** login.jsp is under WEF-INF dir. We can't visit directly.
* when we visit http:localhost:8080/admin/login.html
* with the help of @RequestMapping, according to the returning statement,
* Spring MVC will parse "login" into login.jsp.
*/
@RequestMapping(value="/login.html")
public String loginPage(){
return "login";
}
@RequestMapping(value=".loginCheck.html")
public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand){
boolean isValidUser = userService.hasMatchUser(loginCommand.getUserName(), loginCommand.getPassword());
if(!isValidUser){
// Not match user in db. SpringMVC will parse the ModelAndView into login.jsp with error params.
return new ModelAndView("login", "error", "用户名或密码错误!");
}else{
//match user successfully in db.
User user = userService.findUserByName(loginCommand.getUserName());
user.setLastIp(request.getRemoteAddr());
user.setLastVisit(new Date());
userService.loginSuccess(user);
// When jumping to main.jsp, it contains attribute param of user.
request.getSession().setAttribute("user", user);
//SpringMVC will parse ModelAndView("main") into main.jsp
return new ModelAndView("main");
}
}
}
Maybe you will need to add these in SpringMVC config file of viewspace-servlet.xml
<!-- To handle @RequesttMapping of class level. -->
<bean id="defaultAnnotationHandlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- To handle @RequestMapping of method level. -->
<bean id="annotationMethodHandlerAdapter"
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
If you meet encoding problem, there may be a solution.
<!-- in config file of web.xml -->
<filter>
<filter-name> CharacterEncodingFilter </filter-name>
<filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> utf-8 </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> CharacterEncodingFilter </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
[Note: What I do is only for study. To make me and you master it efficiently.]