代码:
loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>
<!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=UTF-8">
<title>测试基于浏览器请求的国际化</title>
</head>
<body>
<!-- 使用message标签来输出国际化信息 -->
<h3><spring:message code="title"/></h3>
<form:form modelAttribute="user" method="post" action="login">
<table>
<tr>
<td><spring:message code="loginname"/></td>
<td><form:input path="loginname"/></td>
</tr>
<tr>
<td><spring:message code="password"/></td>
<td><form:input path="password"/></td>
</tr>
<tr>
<td><input type="submit" value="<spring:message code="submit"/>"/></td>
</tr>
</table>
</form:form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>
<!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=UTF-8">
<title>测试基于浏览器请求的国际化</title>
</head>
<body>
<spring:message code="welcome" arguments="${requestScope.user.username}"/>
</body>
</html>
User.java
package com.bean;
import java.io.Serializable;
public class User implements Serializable {
private String loginname;
private String password;
private String username;
public User() {
super();
// TODO Auto-generated constructor stub
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "User [loginname=" + loginname + ", password=" + password + ", username=" + username + "]";
}
}
UserController.java
package com.control;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
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.servlet.support.RequestContext;
import com.bean.User;
@Controller
public class UserController {
private static final Log logger = LogFactory.getLog(UserController.class);
@RequestMapping(value="/{formName}")
public String loginForm(
@PathVariable String formName,
Model model){
User user=new User();
model.addAttribute("user",user);
// 动态跳转页面
return formName;
}
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(
@ModelAttribute @Validated User user,
Model model,
HttpServletRequest request){
logger.info(user);
if(user.getLoginname()!=null && user.getLoginname().equals("haige")
&& user.getPassword()!=null && user.getPassword().equals("123456")){
//从后台代码获取国际化信息username
RequestContext requestContext=new RequestContext(request);
String username=requestContext.getMessage("username");
// 将获取的username信息设置到User对象并设置到Model当中
user.setUsername(username);
model.addAttribute("user", user);
return "success";
}
return "error";
}
}
源码:网盘极速下载